Countries API Example
This example demonstrates how to create a simple workflow that fetches data from an external API using the built-in @nanoservice-ts/api-call
node. The workflow retrieves a list of countries and their capitals from a public API and returns the data as a JSON response.
Prerequisites
Before running this example, ensure you have:
- A Nanoservice-ts project set up with the HTTP trigger
- Node.js (v22 or later) and npm installed
If you haven’t created a project yet, you can do so with:
Follow the prompts:
- Provide a name for your project
- Select “HTTP” as the trigger
- Select “NodeJS” as the runtime
- Choose “YES” when asked to install examples
Workflow Structure
The Countries workflow is defined in workflows/json/countries.json
:
This workflow:
- Is triggered by an HTTP GET request to the root path (
/
) - Uses the built-in
@nanoservice-ts/api-call
node to make a GET request to the countries API - Returns the API response directly to the client
Running the Example
-
Start your Nanoservice-ts application:
-
Access the Countries workflow:
Open your browser and navigate to:
Or use curl:
You should receive a JSON response containing a list of countries and their capitals.
How It Works
The Countries workflow demonstrates several key concepts in Nanoservice-ts:
HTTP Trigger
The workflow is triggered by an HTTP GET request to the specified path. In the workflow definition, this is configured in the trigger
section:
Built-in API Call Node
The workflow uses the @nanoservice-ts/api-call
node, which is a built-in node that handles HTTP requests to external APIs. This node is configured with inputs specifying the URL, method, headers, and expected response type:
API Call Node Implementation
The @nanoservice-ts/api-call
node is implemented as follows:
This node:
- Takes inputs for the API call (method, URL, headers, response type, and optional body)
- Makes the API call using the
runApiCall
utility function - Returns the result on success or an error on failure
Response Handling
The response from the external API is automatically returned to the client. The Nanoservice-ts framework handles the serialization of the response to JSON and sets the appropriate content type headers.
Customizing the Example
You can customize the Countries workflow in several ways:
Change the API Endpoint
Modify the url
in the node inputs to fetch data from a different API:
Add Query Parameters
Add query parameters to the URL to filter or customize the API response:
Add Authentication
If the API requires authentication, add the necessary headers:
Add Data Transformation
To transform the API response before returning it to the client, you can add a custom node to the workflow:
- Create a new node for data transformation
- Add it as a step after the API call
- Configure it to process the data from the API call
Conclusion
The Countries workflow example demonstrates how to use Nanoservice-ts to create a simple API proxy. By leveraging the built-in @nanoservice-ts/api-call
node, you can easily integrate external APIs into your applications without writing complex HTTP client code.
This pattern can be extended to create more sophisticated workflows that combine data from multiple sources, transform data, and implement business logic.