DEV Community

hediyeh kianmehr
hediyeh kianmehr

Posted on • Edited on

Creating and Calling APIs in Node-RED

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


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:

GETRetrieve data

POSTAdd new data

PUTUpdate existing data

DELETERemove data


2. How to Build an API in Node-RED (Making APIs)

Node-RED makes it easy to build APIs by combining:

HTTP In nodesDefine API endpoints (like /users/getAll)

Function nodesProcess requests (business logic)

HTTP Response nodesSend results back to the client

Basic flow:

HTTP In → Function → HTTP Response
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

3.2 Get a User by ID

Request:

GET https://codenxa.leanea.com/hediyeh/users/Get/1
Enter fullscreen mode Exit fullscreen mode

Response:

{
  "id": 1,
  "age": 28,
  "first_name": "Hediyeh",
  "last_name": "Karimi",
  "email": "hediyeh.karimi@example.com",
  "country": "Iran"
}
Enter fullscreen mode Exit fullscreen mode

3.3 Add a User

Request:

POST https://codenxa.leanea.com/hediyeh/users/post
Content-Type: application/json
Enter fullscreen mode Exit fullscreen mode

Body:



{
  "age": 25,
  "first_name": "Sara",
  "last_name": "Ahmadi",
  "email": "sara.ahmadi@example.com",
  "country": "Iran"
}
Enter fullscreen mode Exit fullscreen mode
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" }
]
Enter fullscreen mode Exit fullscreen mode

3.4 Update a User

Request:


PUT https://codenxa.leanea.com/hediyeh/users/put/2
Content-Type: application/json
Enter fullscreen mode Exit fullscreen mode

Body:

{
  "age": 21,
  "email": "ali.new@example.com"
}
Enter fullscreen mode Exit fullscreen mode

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" }
]
Enter fullscreen mode Exit fullscreen mode

3.5 Delete a User

Request:

DELETE https://codenxa.leanea.com/hediyeh/users/delete/4
Enter fullscreen mode Exit fullscreen mode

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" }
]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Node-RED Flow

Inject nodetriggers the request

HTTP Request nodeconfigured with the API URL

Debug nodeshows the response

Response
{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "city": "Gwenborough",
    "zipcode": "92998-3874"
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Node-RED Flow

Inject node → starts the request
Enter fullscreen mode Exit fullscreen mode

HTTP Request nodeconfigured with https://codenxa.leanea.com/hediyeh/users/GetAll

Debug nodeshows 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"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

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 APIWhen you want others to use your system (e.g., CRUD on your user database).

Calling APIWhen you need data from external systems (e.g., weather API, payment gateway, external DB).


Top comments (0)