Using Tyk to combine multiple API calls


#1

Hello. I’m trying to use Tyk as a gateway that can take in a single request and perform multiple calls to another API and return a specially crafted result.
e.g.
User sends request to ‘http://tykmachine:8080/test/createMeal
tyk calls to my back-end API methods, ‘createStarter’, ‘createMain’, ‘createDesert’, takes the created IDs and calls ‘createFullMeal’ and returns a response with the ID of final created meal item and the other pieces created.

From reading the site it seems like Virtual EndPoints are my best option, listening on a path and specifying a JavaScript file that can then make calls to the API. I have some questions about this.

  1. I can’t hardcode the address of my back-end API into the JavaScript files. The API is a Docker container that will run in many different environments so no fixed IP or hostname is available and it isn’t feasible to have people update the urls in multiple JavaScript files. What is the best way to provide the address of the back-end API for use in the JavaScript file? The issue of config has come up previously in the thread but I’m hoping there may be some way to pass even the ‘target_url’ from the app config file since creating a service to provide configuration information is leading us away from the reason for using Tyk (to simplify our APIs).

  2. Are there any suggestions for debugging the JavaScript endpoints? The docs suggest using ‘log’ however a call like ‘log(config)’ just outputs “[object Object]” which is of limited use. Currently I’m planning to isolate my JS and test it separately, mocking up the parameters provided by Tyk and importing the API functions available but was wondering if there’s any better advice?

The Tyk Gateway is running as a Docker container, as is the back-end API.

Thanks for any assistance you can provide.


#2

You could pre-load the data, since the JS is initialised when the API loads, you could put a global function into the file that gets called and that will pull the API Definition object for the API ID via the dashboard (then you are exposing an API token that might only have read-only access to the API Defs in the code). Then that var is available within the virtual endpoint. Could be tricky, since you need that to run first so that the name is registered in the NS.

We use Otto, which you can install with a CLI to debug your JS? You’d need to mock the JSVM API though.