Overview
This document explains how to create (make) and call (consume) APIs in Node-RED.
It covers:
- The basics of what an API is.
- How to build APIs in Node-RED (CRUD example).
- How to call APIs from Node-RED (both external and internal).
- Differences between making and calling APIs.
- Practical examples with requests & responses.
- A clear comparison of when to use each approach.
By the end, you’ll understand how Node-RED can act both as an API server and an API client.
Table of Contents
- What is an API?
- How to Build an API in Node-RED (Making APIs)
- Example Requests & Responses
- API Calling (Consuming APIs)
- Difference Between Making vs. Calling APIs
- When to Use What?
1.What is an API?
An API (Application Programming Interface) allows different applications to communicate with each other.
In web development, APIs are usually HTTP-based, meaning you can use HTTP methods like:
GET → Retrieve data
POST → Add new data
PUT → Update existing data
DELETE → Remove data
2. How to Build an API in Node-RED (Making APIs)
Node-RED makes it easy to build APIs by combining:
HTTP In nodes → Define API endpoints (like /users/getAll)
Function nodes → Process requests (business logic)
HTTP Response nodes → Send results back to the client
Basic flow:
HTTP In → Function → HTTP Response
Practicing User CRUD API in Node-RED
Method | Endpoint | Description |
---|---|---|
GET | /users/GetAll |
Get all users |
GET | /users/Get/:id |
Get a single user by ID |
POST | /users/post |
Add a new user |
PUT | /users/put/:id |
Update a user by ID |
DELETE | /users/delete/:id |
Delete a user by ID |
3.Example Requests & Responses
3.1 Get All Users
Request:
GET https://codenxa.leanea.com/hediyeh/users/GetAll
Response:
{
"users": [
{
"id": 1,
"age": 28,
"first_name": "Hediyeh",
"last_name": "Karimi",
"email": "hediyeh.karimi@example.com",
"country": "Iran"
},
{
"id": 2,
"age": 20,
"first_name": "Ali",
"last_name": "Rezaei",
"email": "ali.rezaei@example.com",
"country": "Iran"
}
]
}
3.2 Get a User by ID
Request:
GET https://codenxa.leanea.com/hediyeh/users/Get/1
Response:
{
"id": 1,
"age": 28,
"first_name": "Hediyeh",
"last_name": "Karimi",
"email": "hediyeh.karimi@example.com",
"country": "Iran"
}
3.3 Add a User
Request:
POST https://codenxa.leanea.com/hediyeh/users/post
Content-Type: application/json
Body:
{
"age": 25,
"first_name": "Sara",
"last_name": "Ahmadi",
"email": "sara.ahmadi@example.com",
"country": "Iran"
}
Response:
[
{ "id": 1, "first_name": "Hediyeh", "last_name": "Karimi", "age": 28, "email": "hediyeh.karimi@example.com", "country": "Iran" },
{ "id": 2, "first_name": "Ali", "last_name": "Rezaei", "age": 20, "email": "ali.rezaei@example.com", "country": "Iran" },
{ "id": 6, "first_name": "Sara", "last_name": "Ahmadi", "age": 25, "email": "sara.ahmadi@example.com", "country": "Iran" }
]
3.4 Update a User
Request:
PUT https://codenxa.leanea.com/hediyeh/users/put/2
Content-Type: application/json
Body:
{
"age": 21,
"email": "ali.new@example.com"
}
Response:
[
{ "id": 1, "first_name": "Hediyeh", "last_name": "Karimi", "age": 28, "email": "hediyeh.karimi@example.com", "country": "Iran" },
{ "id": 2, "first_name": "Ali", "last_name": "Rezaei", "age": 21, "email": "ali.new@example.com", "country": "Iran" }
]
3.5 Delete a User
Request:
DELETE https://codenxa.leanea.com/hediyeh/users/delete/4
Response (remaining users):
[
{ "id": 1, "first_name": "Hediyeh", "last_name": "Karimi", "age": 28, "email": "hediyeh.karimi@example.com", "country": "Iran" },
{ "id": 2, "first_name": "Ali", "last_name": "Rezaei", "age": 20, "email": "ali.rezaei@example.com", "country": "Iran" }
]
4.API Calling (Consuming APIs)
Besides making APIs, Node-RED can also consume external APIs (call them like a client).
This is done with the HTTP Request node.
Basic flow:
Inject → HTTP Request → Debug
4.1 Example: Calling a Public API
Request
Suppose we want to call the JSONPlaceholder
fake API to get user info:
GET https://jsonplaceholder.typicode.com/users/1
Node-RED Flow
Inject node → triggers the request
HTTP Request node → configured with the API URL
Debug node → shows the response
Response
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"city": "Gwenborough",
"zipcode": "92998-3874"
}
}
4.2 Example: Calling an Internal API (Inside Node-RED)
You can also use Node-RED’s HTTP Request node to call APIs you created in Node-RED itself.
For example, if you already built a User API (/users/GetAll)
, you can call it from another flow:
Request
GET https://codenxa.leanea.com/hediyeh/users/GetAll
Node-RED Flow
Inject node → starts the request
HTTP Request node → configured with https://codenxa.leanea.com/hediyeh/users/GetAll
Debug node → shows the result
Response (from your own Node-RED API)
{
"users": [
{
"id": 1,
"age": 28,
"first_name": "Hediyeh",
"last_name": "Karimi",
"email": "hediyeh.karimi@example.com",
"country": "Iran"
},
{
"id": 2,
"age": 20,
"first_name": "Ali",
"last_name": "Rezaei",
"email": "ali.rezaei@example.com",
"country": "Iran"
}
]
}
Note:
External APIs (like JSONPlaceholder, weather APIs, payment APIs).
Internal APIs (APIs you built inside Node-RED).
5. Difference Between Making vs. Calling APIs
Making API (Server role) | Calling API (Client role) |
---|---|
Node-RED creates endpoints (like /users/... ) |
Node-RED sends requests to endpoints |
Uses HTTP In + Function + HTTP Response | Uses Inject + HTTP Request + Debug |
Responds to external clients (Postman, apps, etc.) | Fetches data from external services (APIs) |
Node-RED behaves as a server | Node-RED behaves as a client |
6. When to Use What?
Making API → When you want others to use your system (e.g., CRUD on your user database).
Calling API → When you need data from external systems (e.g., weather API, payment gateway, external DB).
Top comments (0)