Creating a REST API

By the end you have a complete tasks resource: list, read, create, update and delete, with proper methods, statuses, validation and scopes.

A resource is five endpoints

OperationMethod and URISuccess status
ListGET /api/v1/tasks200 and an array
CreatePOST /api/v1/tasks201 and the created document
Read oneGET /api/v1/tasks/{guid}200 and the document, 404 if unknown
UpdatePUT /api/v1/tasks/{guid}200 and the updated document, 404 if unknown
DeleteDELETE /api/v1/tasks/{guid}200 or 204, 404 if unknown

Each row is one API block (on_startup_register_uri_get, on_startup_register_uri_post, on_startup_register_uri_put, on_startup_register_uri_delete) feeding one handler function. The URI on the block is tasks or tasks/:guid; a :name segment arrives through httpconnection_get_params. This is the same shape as in Block Templates.

GET /tasks lists with filters and paging

  1. Register it

    on_startup_register_uri_get (On API GET) with URI tasks, connected to a handler function.

  2. Read the query string

    httpconnection_get_query (get query) holds the optional status, limit and offset. Read them with object_field_getter_multiple (get fields); a number-typed pin parses the text for you.

  3. Build the query

    query_bool_term_fields (fields) on status into query_bool_filter (filter), and query_sort_field (sorting) on _meta.created descending, merged by query_and (multiple). Only filter when a status was sent: condition_not_empty_value (is not empty) on it into query_branch (if), because an empty term matches nothing.

  4. Search

    function_search (search) on the tasks storage with the limit and offset pins. Unwired, limit is 5000, so always wire it.

  5. Reply

    httpconnection_set_json (reply in JSON) with code 200 and the results array. Wire its fields pin to ship only the fields you list.

POST /tasks validates before it creates

  1. Register it

    on_startup_register_uri_post (on api post) with URI tasks.

  2. Validate the body

    Read the fields with httpconnection_get_body (get body) and object_field_getter_multiple. For each required field, condition_not_empty_value into a branch (branch); on false, reply 400 with {"error": "title is required"}. See the validation template.

  3. Create the document

    Map the body to document fields with mutations_set_bp_field_multiple (set fields), then function_create_document_from_mutations (create document).

  4. Reply

    201 with the block's object, which now has its _meta.guid.

GET /tasks/{guid} answers 200 or 404

  1. Register it

    on_startup_register_uri_get with URI tasks/:guid. Read guid from httpconnection_get_params (get params).

  2. Fetch it

    function_get_document (get document) on the tasks storage. Continue from its found flow pin.

  3. Answer both paths

    branch on the accessible pin, which is true when the document exists and is not removed. True: reply 200 with object. False: reply 404 with {"error": "not found"}. See error handling.

PUT and DELETE guard the same way

Set the scopes, then test every row

  • Scope each block with the lock icon: *public for open reads, *loggedin or a custom scope for writes. A route without a scope is not public: it answers 403. See Remote Access Control.
  • Activate the blueprint, then try every row of the table. More commands are in API workflows.
  • When an endpoint 404s right after activation, it is almost always a scope or an inactive blueprint. See Common Issues.
Create a task, then list
curl -X POST "https://<cluster-url>/api/v1/tasks" \
  -H "Authorization: Bearer <access_token>" \
  -H "Content-Type: application/json" \
  -d '{"title": "write the docs"}'

curl "https://<cluster-url>/api/v1/tasks?status=open&limit=10" \
  -H "Authorization: Bearer <access_token>"

Next steps

API Guide Conventions, error codes, and rate limits for your API. Storages The queries and mutations behind every row of the table. Block Templates The endpoint and validation shapes in one view.

Frequently asked

How do I build a CRUD REST API in RUAL?

One API block per operation: GET and POST /api/v1/tasks for list and create, and GET, PUT and DELETE /api/v1/tasks/{guid} for read, update and delete. Each on_startup_register_uri block feeds one handler function that does the work and replies with the right status: 200 for reads, 201 for creates, 404 for unknown guids.

How do I validate the request body in a RUAL API?

Check each required field with its own condition_not_empty_value block into a branch, and reply 400 on the false path before anything is stored. On the success path, map the body to document fields with mutations_set_bp_field_multiple and create the document from those mutations.

Should a RUAL DELETE endpoint remove or delete the document?

Your choice: function_remove_document soft-deletes and stays recoverable, while function_delete_document hard-deletes when you mean it. Reply 200 with a small confirmation object either way.