Creating API Service
In this short guide we will showcase how to instantiate the nanoservice-ts framework and run a simple workflow.
In this example we will create an API that will get the list of all space launches that happened in the year a person was born. The API will accept one parameter dob: string
as date of birth and return a json
list of space launches.
Initialilizing
Create project
nanoctl
package for nanoservice-ts is a command-line tool designed to simplify the creation and setup of nanoservice projects. It enables developers to quickly initialize ready-to-use project structures with minimal configuration. To learn more about available commands, visit nanoctl To initialize the project, simply run the following command and follow the instructions of the interactive setup:
Run the command
Follow the interactive prompts
After creating a project using the npx package, your folder might look like this:
Key Directories:
workflows/
: Contains your nanoservice workflow definitions.src/
: Your project’s main logic and entry point.src/nodes/
: Contains custom nodes that extend functionality.
Creating nodes
For this project, we will create 1 custom node:
api-call
- a node for fetching data from an external API.
Setup
To create your first node, run the following command:
Run the command
Follow the interactive prompts
Install dependencies and start coding
After running the command, a new directory was added to the src/nodes/
folder under the new node’s name with all the necessary files to start coding your custom node:
Defining node logic
index.ts
file is the main file for your custom node. It contains the logic that will be executed by the node during workflow invocation.In the nodes/api-call/index.ts
we will create a node that will fetch data from an external API. For the simplicity of this example it will be hardcoded for only GET
requests with headers set to application/json
, will receive a URL as an input and return the response.
Add node configuration (optional)
config.json
file is used to define the expected inputs and outputs for your custom nodes. It provides a structured way to specify the input and output formats, required fields, and other data. config.json
file is optional as it is only used by the Deskree platform to connect nodes together. It will be used in the future for other purposes within the framework. Hence, we still recommend creating it as the best practice.Let’s set up config.json
files for both of the nodes we’ve created. For the simplicity of this example, we will define only inputs and outputs, but you can learn more about the advanced configuration options in the Node Configurations.
For the api-call
node, the config.json
file should look like this:
Build the new Node
nanoservice-ts
imports the created nodes at runtime. It is required to build the node using the script build.To build the node run the following command:
Install dependencies and run build
After executing the build command a new dist
directory will be created in the node’s directory with the compiled files.
Creating a workflow
Now let’s create a workflow that will use the nodes we’ve created. Create a new file in the workflows
directory called launches-by-year.json
.
Note that in this example we are using ctx
to get the original request query parameter dob
and pass it to the get-year
node. The get-year
node will extract the year from the date of birth and pass it to the final response.
Running Workflow
To test our newly created workflow simply run a command npm run dev
in the root directory of the project. This will start the server on the port specified in the .env.local
file. By default, you should be able to access at port 4000
.
Then make a GET API request to http://localhost:4000/launches-by-year either using a browser or a tool like Postman. The request should contain a query parameter dob
with the date of birth in the format YYYY-MM-DD
. For example:
The response should be a JSON object with the list of space launches that happened in the year 1990.
Summary
You have successfully created a simple API service using nanoservice-ts
. You have learned how to create custom nodes, define a workflow, and run it.