Storing Variables and Using them in API Requests

In many cases, it might be useful to make several API requests in succession where we use data obtained from one request & feed it into another request. Such examples may include registering a new user & obtaining an authentication token for subsequent requests, or placing an order and retrieving the order details for further processing, and so on.  

With Sofy’s API Step you can store data obtained from one step and use it across multiple API steps by passing it the variable in the body, header, query parameter or as a path variable. Here’s how you can do it. 

Using variables in the Body of an API request

  1. Send an API request & evaluate the Response obtained. Here, we send a POST request to https://reqres.in/api/register for registering a new user: 
     
  2. From the Response obtained store the variable which you wish to use across API steps by clicking on the “+” icon next to it & defining a name for it. For our example, we will store the “token” obtained in the response and call it “userToken”: 
     
  3. Next, we will try to use this stored variable across the next API step. Here, we will try to create a profile for this user. For this, we send a POST request to https://reqres.in/api/profiles and send the token obtained from previous API request in the Request Body: 
     
  4. Once we send the request & add the API Step, we will observe that a successful Response may be received indicating that the stored variable was used successfully: 

Using variables in the Header of an API request

Similar to how you captured data inside variables & use them inside Request Body for subsequent API requests (above), you could also use these variables in the Header.

We can send captured variables inside Headers which may provide additional information about the request or the client making the request such as metadata, authentication tokens, content type and so on. For example, here we are capturing the data related to "id" and storing it in a variable, which we later pass in the Header for the next API request:

Using variables as a Query Parameter of an API request

Similarly, we could also capture data in variables which we use as Query Parameters. Query Parameters may be useful specifying additional information or filtering data in the request and are generally used for filtering, searching, or pagination.

Using variables as a Path Variable of an API request

Path Variables are useful when you need to pass dynamic values in the URL, such as IDs or resource names. As an example here, we first send a GET request to obtain a list of all users & storing the ID of a particular user in a variable called "id":

Once you store a variable (in our case "id"), we can then use it as a Path Variable while sending a new API Request. You can click on the Path Variables tab inside the API Modal and add a Path Variable by selecting an already defined variable from the dropdown ("id"):

Once you add it in the API Step and send a request, this path variable will then allow you to fetch a response based on its value (i.e. value of the "id"):

Response after entering the stored variable in Parth Variables


How did we do?