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
| Operation | Method and URI | Success status |
|---|---|---|
| List | GET /api/v1/tasks | 200 and an array |
| Create | POST /api/v1/tasks | 201 and the created document |
| Read one | GET /api/v1/tasks/{guid} | 200 and the document, 404 if unknown |
| Update | PUT /api/v1/tasks/{guid} | 200 and the updated document, 404 if unknown |
| Delete | DELETE /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
Register it
on_startup_register_uri_get(On API GET) with URItasks, connected to a handler function.Read the query string
httpconnection_get_query(get query) holds the optionalstatus,limitandoffset. Read them withobject_field_getter_multiple(get fields); a number-typed pin parses the text for you.Build the query
query_bool_term_fields(fields) onstatusintoquery_bool_filter(filter), andquery_sort_field(sorting) on_meta.createddescending, merged byquery_and(multiple). Only filter when a status was sent:condition_not_empty_value(is not empty) on it intoquery_branch(if), because an empty term matches nothing.Search
function_search(search) on thetasksstorage with thelimitandoffsetpins. Unwired,limitis 5000, so always wire it.Reply
httpconnection_set_json(reply in JSON) with code 200 and the results array. Wire itsfieldspin to ship only the fields you list.
POST /tasks validates before it creates
Register it
on_startup_register_uri_post(on api post) with URItasks.Validate the body
Read the fields with
httpconnection_get_body(get body) andobject_field_getter_multiple. For each required field,condition_not_empty_valueinto abranch(branch); on false, reply 400 with{"error": "title is required"}. See the validation template.Create the document
Map the body to document fields with
mutations_set_bp_field_multiple(set fields), thenfunction_create_document_from_mutations(create document).Reply
201 with the block's
object, which now has its_meta.guid.
GET /tasks/{guid} answers 200 or 404
Register it
on_startup_register_uri_getwith URItasks/:guid. Readguidfromhttpconnection_get_params(get params).Fetch it
function_get_document(get document) on thetasksstorage. Continue from itsfoundflow pin.Answer both paths
branchon theaccessiblepin, which is true when the document exists and is not removed. True: reply 200 withobject. False: reply 404 with{"error": "not found"}. See error handling.
PUT and DELETE guard the same way
- PUT:
on_startup_register_uri_put(on api put), the same fetch-first guard, thenfunction_update_document_mutations(update document) with the changed fields. Reply 200 with the updated document, 404 when missing. - DELETE:
on_startup_register_uri_delete(on api delete), then soft-delete withfunction_remove_document(remove document) (recoverable), or hard-delete withfunction_delete_document(permanently delete document) when you mean it; that block refuses unless itsvery_surepin is true. See document lifecycle. Reply 200 with a small confirmation object.
Set the scopes, then test every row
- Scope each block with the lock icon:
*publicfor open reads,*loggedinor 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.
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.