Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

How do I reuse authenticated APIs as a datasource?

Issue

I am struggling with configuring a common variable, such as a domain, for multiple APIs. The domain is the same for all APIs, but there are differences for multiple environments. I want to find a way to configure this variable so that I don't have to keep entering it for each API.

Resolution

To configure a common variable like domain for multiple APIs, you can create a datasource and configure the domain URL for that datasource. This can be done through the API management platform or code. Once the datasource is created, you can create new APIs for that datasource and use the same domain URL without needing to enter it again. This allows you to easily manage multiple APIs with the same domain and configure them for different environments. An example of configuring a datasource in code using Node.js may look like this:

const Swagger = require("swagger-client");

const domainUrl = process.env.DOMAIN_URL || "http://localhost:3000";

const api = new Swagger({
url: `${domainUrl}/api-docs`,
requestInterceptor: (req) => {
req.url = `${domainUrl}${req.url}`;
return req;
},
});

module.exports = api;

Here, we are setting the domainUrl variable based on the environment variable or a default URL. We then create a new Swagger client for the API and configure the URL using the domainUrl variable. The requestInterceptor method updates the URL for each request to use the configured domainUrl. This allows us to easily switch between different environments by updating the domainUrl variable.