API keys and access tokens
API keys and access tokens are crucial for secure communication with APIs. API keys identify applications, and access tokens authenticate and validate requests, ensuring secure data exchange between applications and APIs
Add API key authorization
To add an API key:
Go to Automation > API > click the particluar API > Headers > + Add Headers.
In Key enter Authorization and in Value enter the API key. Click Add.
Click Send to test the API, if you recieve a 200 response the API has been authorized and works successfully.
Build a flow for your use case and include the API node wherever you want the flow to hit the API. Fetch this API in the node and build the rest of the flow.
Pass dynamic access tokens
To fetch a dynamic access token:
Add the API. In Key enter Authorization and in Value enter the variable name in {{{}}} format, for example {{{auth_token}}}.
Go to Functions and create a new function that filters out the access token from the response.
Sample code:
let { apiResponse } = ymLib.args; // retrieve API response
let token = JSON.parse(apiResponse.body);
console.log(token);
return token.token_type + " " + token.access_token;
Build a flow for your use case and include the API node wherever you want the flow to hit the API.
Scroll down to Parse using function and add the following:
- Select variable: Create/choose the variable in which you want to store the parsed response. Here the variable would be auth_token.
- Add function: Choose the function created in step
Include another API node and add/or fetch the API (created in step 1). In the Dynamic variable, fetch the auth_token variable (created in step 5).
By this way you can authenticate the API and use the dynamic access token throughout the flow to authenticate different APIs from the same server.