A REST API in Node-RED is an HTTP interface you build by wiring browser or device requests to flows using the built-in http in, function, and http response nodes. Node-RED turns a URL path such as /api/temperature into a set of message routes, and each route returns a status code and a body. In practice, one request comes in, your flow decides what to do, and one response goes back out.

A concrete example makes it clear. You drag an http in node onto the canvas, set its method to GET and its URL to /api/temperature, connect it to a function node that sets msg.payload to a small object, then connect that to an http response node. When a browser calls http://your-host:1880/api/temperature, the flow replies with 200 OK and the object as JSON. That is the whole pattern. Everything else is decoration around it.

Two details decide whether the flow works. The http in node must connect through to an http response node on the same route, or the caller waits until it times out. Node-RED also needs msg.res left intact, so avoid copying or resetting the whole message before the response node sees it.

Build the Route With Three Nodes

Node-RED ships with the HTTP nodes, so no extra installation is needed. They live in the network category of the palette.

  1. Drag an http in node onto the canvas. Double-click it and choose the method. Use GET for reading data, POST for creating something, PUT or PATCH for updating, and DELETE for removing.
  2. Set the URL field. A path such as /api/readings is the endpoint. You can put a colon in the path, like /api/readings/:id, to capture part of the URL as a parameter. That value arrives on msg.req.params.id.
  3. Add a function node. This is where you read the request and build the reply. For a GET, set msg.payload to the data you want to return. To read a query string like ?limit=10, use msg.req.query.limit. To read a JSON body on a POST, use msg.payload, since the HTTP nodes parse JSON bodies for you when the content type is JSON.
  4. Add an http response node and wire the function output into it. Set the status code to 200 for a normal reply.
  5. Open the URL in a browser or call it with any HTTP client. A GET should return your payload immediately.

Labels in the node dialog can differ slightly between Node-RED versions, so check the field names as you go. The node types have stayed the same through many releases.

One Request, One Response, Nothing Shared

Each incoming request creates its own message. That means ten callers at once produce ten separate messages, and msg.res on each one points back to its own caller. This is why a function node must not store over the inbound message and reuse it later. If you need to hold data between requests, put it in a change node writing to flow context, or in an external store, and read it fresh each time. A reply built from stale shared state is the most common source of confusing REST behaviour in Node-RED.

Handle Errors and Send the Right Status Code

A REST caller relies on the status code before it reads the body. Returning 200 with an error message inside the payload breaks that contract, and clients will treat the call as successful.

The simplest approach is a second function node that builds error responses. Set msg.statusCode before the message reaches the response node, and Node-RED uses it. For example, a missing record should return 404, a bad body should return 400, and an unexpected failure should return 500. Keep the body small: an object with error and message fields is enough for most callers to act on.

Use a switch node after your main function to route successful replies to an http response node and failures to an error handler. That keeps the happy path readable. Also remember that an http in node accepts an optional name for its response. If you name it, you can later answer from anywhere in the flow using a http response node with the same name. This helps with slow work such as a device read that takes a few seconds, because you can reply first and let the rest of the flow continue.

Nothing in the flow needs a special framework. Node-RED itself is the web server. The default port is 1880, set in the settings file, and endpoints share that port unless you run more than one instance behind a proxy.

Pick GET, POST and PUT by What the Caller Needs

The method is a decision about meaning, not a preference. Choose it from what the caller is doing, then keep the flow consistent with that choice.

  • GET reads data and should never change anything on the server. Callers and caches assume a GET is safe to repeat.
  • POST creates something or triggers an action. The body carries the details, and the reply should include the new item or its identifier.
  • PUT replaces a whole record, while PATCH changes part of it. Pick one and document it, because clients built against the wrong verb break slowly and quietly.
  • DELETE removes a record and usually returns 204 with an empty body.

A common misconception is that the http in node needs a separate server or an Express app behind it. The correct fact is that Node-RED already exposes an HTTP server, and the node registers your route on it directly. You write flows, not server code.

An Example with Real Numbers

Say you store the last 60 temperature readings, one per minute, in flow context. A caller asks GET /api/readings?limit=10.

  1. The http in node sends the request onward on the /api/readings path.
  2. The function node reads Number(msg.req.query.limit), which is 10. If the query is missing or not a number, use a default of 1 and clamp it between 1 and 60.
  3. The function takes the stored array and slices the last ten entries with slice(-10).
  4. It sets msg.statusCode to 200 and msg.payload to an object holding count: 10 and the array.
  5. The http response node returns that object as JSON, typically a few hundred bytes.

If the caller asks for limit=100, the clamp turns it into 60, so the reply contains all 60 stored readings rather than an error. If the store is empty, return 200 with count: 0 and an empty array. That is easier for clients to handle than a 404, because the endpoint exists and simply has no data yet.

Test the Endpoint Before Anything Else Uses It

Open the URL in a normal browser tab for every GET route. The browser shows the raw JSON, which makes malformed payloads obvious. For POST and PUT, use a tool that can send a JSON body and set the content type header to application/json. Node-RED relies on that header to parse the body, so a missing header turns msg.payload into a plain string and your function may fail to read its fields.

Keep a debug node attached to the function output while testing, set to show the whole message. It reveals msg.req details such as params, query, headers and body, and it confirms the status code before the reply leaves. Remove or disable the debug output once the route is stable, or the debug panel fills up on every call.

Decide early whether the endpoint is public. If it is reachable beyond your own network, add authentication or put it behind a proxy that handles it. Node-RED's editor settings control access to the flow builder, not to the endpoints your flows expose, so treat them as two separate jobs.

Keep Flows Readable When Endpoints Multiply

A single route is easy to follow. Twelve routes on one canvas become hard to trace, and mistakes creep in where two flows share a path or a response node is missed. Give each endpoint its own http in node and connect them to shared subflows only when the logic is genuinely the same.

Name nodes after the route, not after the node type. A response node called readings 200 tells you more than http response 3. Group related routes on separate flow tabs, one tab per resource. If two http in nodes use the same method and the same URL, the request goes to whichever is registered, not to both, so a duplicate path is a silent conflict rather than an error.

For slow endpoints, reply first and finish the work afterwards. Send the successful status and a short body in a few milliseconds, then let the rest of the flow write to storage or call a device. Callers get a fast answer, and a device that is offline does not hold the request open until it times out.

Questions That Come Up While Building Routes

Where does the request body go? A JSON POST body arrives on msg.payload as a parsed object, provided the caller sets the content type header. Form bodies arrive as an object too, but check the shape with a debug node once rather than assuming.

How do you return a plain string or HTML page? Set msg.payload to the string and set the header msg.headers['content-type'] to text/plain or text/html. Without a header, the response node sends JSON.

Why does the caller hang with no reply? Almost always the flow ends without reaching an http response node, or an exception stops the function mid-way. Attach the catch node to your function and return 500 from it so callers get a clear failure instead of a timeout.

Can one flow answer several URLs? Yes, several http in nodes can feed one function, and you can branch on msg.req.method or the path. For anything beyond two or three routes, separate flows stay easier to read and debug.