Create your first function
Build a function that answers POST /hello with a greeting, reading the name from the request body, and publish it as an API.
Steps
-
Create the function
Open Computing › Functions and click New Function. Type a name, for example
hello, and leave the other settings as they are. In the Blocks section click Open visual editor.Expected resultThe visual editor opens on an empty canvas.
-
Read the request
From the Available blocks bar, category Input, drag Incoming event onto the canvas, then drag Body. Connect Incoming event › Event to Body › Event: press on the round port on the right of the first block and release on the port on the left of the second.
Expected resultA line connects the two blocks.
-
Build the greeting
From the Transform category drag Template and connect Body › Body to Template › Data. Select the block and, in the Properties panel, set Template to
Hello, {{name}}!. -
Return the answer
From the Output category drag Response and connect Template › Text to Response › Value.
-
Test the function
Click Run (▶) or press Ctrl+Enter. In the Set the test input dialog write:
{"body": {"name": "Ada"}}and click Run.
Expected resultThe blocks turn green one after the other and the Return value view shows Hello, Ada!
-
Save
Click Save (Ctrl+S) to stay in the editor, or Save and exit to go back to the list.
-
Publish it as an API
Open API › Endpoints and create a
POST /helloendpoint. Set its action to Function and choosehello.Expected resultEvery call to the endpoint starts the function: the request becomes its incoming event and the value reaching Response becomes the response body.
Check
Call the endpoint with an HTTP client, for example:
curl -X POST <endpoint-address>/hello \
-H "Content-Type: application/json" \
-d '{"name": "Ada"}'The answer is Hello, Ada!. The run you just made is listed in Computing › Functions › Executions.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
| The test dialog does not open | The graph has no Incoming event block | Add Incoming event (Input category) and connect it |
| The answer is empty | A connection is missing or goes to the wrong port | Check the Body › Template › Response chain |
| You need a JSON answer | Template outputs text | Write the template as JSON and add Parse JSON (Transform) before Response |
Pieces used
- Incoming event Coming soon
- Body Coming soon
- Template Coming soon
- Response Coming soon
Next steps
- Condition
- How a run works Coming soon
- Expose a function as an API Coming soon