How to call 2 endpoints

Hi Experts,

I am trying to figure out if below use case is possible using tyk,

Use Case
Sales Force (API provider) -> Tyk -> Backend System (API consumer)

In order to consume API provided by Sales Force, first we need to get access token using url Endpoint1 and then with the access token call Endpoint2 of sales force.

Trying to achieve
Will expose only one API from Tyk towards Backend system. When Backend sends the request towards Tyk, Tyk will first call endpoint1 (with custom inputs) of Sales force to get access token and then append that access token in the request which came from backend and send the updated request towards Sales force using endpoint2.

Request you to please let me know if it is possible.

Note: Both endpoints are completely different

You can achieve this by using virtual endpoints, there’s a detailed sample here.

The virtual endpoint could send these two requests, prepare the response and send it to the client.

Hi @matiasb

Have gone through the link you have shared.
What I understand now is,
Tyk will expose one api, when request comes to the api first endpoint will get invoked which is mention in the target url of core settings, and then the virtual endpoint code. [ Please correct me if my understanding is wrong]

Issue what I see here is , request accepted by sales force for generating access token is in application/x-www-form-urlencoded. Code sample is sharing JSON.

Please help me out in knowing how the content type application/x-www-form-urlencoded can be send in request

Hi @matiasb

With virtual endpoint I was not able to call the endpoint.

Though have tried something else,

  1. Created one middleware to access endpoint1
  2. Created one api, configured to access endpoint2 and pre middleware as created above.

Now I need to pass the response received in from middleware to this API, how can I achieve that?

Or let me know if this is not possible.

You should set the Content-Type header, based on the sample above, the request could be:

  newRequest = {
    "Method": "GET",
    "Body": "",
    "Headers": {"Content-Type": "application/x-www-form-urlencoded"},
    "Domain": "http://httpbin.org",
    "Resource": "/headers",
    "FormData": {}
  };

To send the request and get the body:

rawResponse = TykMakeHttpRequest(JSON.stringify(newRequest));
response = JSON.parse(rawResponse);

You should be able to access the body using response.Body, to print it:

log(response.Body)

You can use this body to construct a new request and send it over with second TykMakeHttpRequest statement.

Best.

Hi @matiasb,

Thank you for the guidance, was now able to successfully two calls using JS function from virtual endpoint.

Though I have one doubt, while returning back the response from JS function, how can we send the complete header which is received from the actual endpoint call.

I am using the below code for sending the final response back,

          rawCallbackResponse = TykMakeHttpRequest(JSON.stringify(callBackRequest));
          callbackResponse = JSON.parse(rawCallbackResponse);
          log(callbackResponse.Body)
          var responseObject = {
          Body:callbackResponse.Body,
          Headers:{},
          Code:callbackResponse.Code
          }
          return TykJsResponse(responseObject, session.meta_data)

What should I use for sending the complete header?