Spanish Total.js
English
Documentation For AI agents
Contents

Create a GET endpoint

Build an HTTP endpoint that reads a query argument and answers with JSON.

.md GitHub
On this page

This endpoint answers GET /api/hello/. It greets the name you pass in the address, or the world when there is none.

Route, Code and Response nodes connected for the hello endpoint

You need the Route, Code and Response components. See Download components.

Build it

  1. Drag Route, Code and Response onto the canvas.
  2. Connect Request of Route to Input of Code, and Output of Code to Response of Response.
  3. Double-click Route. Keep Method GET, set Relative URL address to /api/hello/ and select APPLY.
  4. Double-click Code, replace the code with the code below and select APPLY.
  5. Double-click Response, empty Path for reading value and select APPLY.
  6. Select APPLY in the toolbar, then Continue.

The code of the Code node:

send({ message: 'Hello, ' + (value.query.name || 'world') + '!' });

value is the request that Route sends: with Send to output set to Everything, the query arguments are in value.query.

Try it

curl 'http://localhost:8000/api/hello/?name=Jane'
{"message":"Hello, Jane!"}

Without a name:

curl http://localhost:8000/api/hello/
{"message":"Hello, world!"}

Why empty the path

By default, Response sends only the response property of the data it receives. Empty Path for reading value to send the whole object, or send your data inside response, as in Create a POST endpoint.

See also Route and Response.

Made in Europe