Documentation navigation
How-to

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.

For
First-time OpenBack users
Time
10 minutes
You need
A project and permission to create functions
Outcome
An endpoint that answers Hello, Ada! to {"name": "Ada"}

Steps

  1. 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.

  2. 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.

  3. 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}}!.

  4. Return the answer

    From the Output category drag Response and connect Template › Text to Response › Value.

  5. 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!

  6. Save

    Click Save (Ctrl+S) to stay in the editor, or Save and exit to go back to the list.

  7. Publish it as an API

    Open API › Endpoints and create a POST /hello endpoint. Set its action to Function and choose hello.

    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

SymptomCauseFix
The test dialog does not openThe graph has no Incoming event blockAdd Incoming event (Input category) and connect it
The answer is emptyA connection is missing or goes to the wrong portCheck the Body › Template › Response chain
You need a JSON answerTemplate outputs textWrite 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