HTTP calls
The wire contract for calling a handler without a client, with its path, key encoding, credentials, headers, status codes and error body.
Every handler of a luna can be called with one HTTP request: POST to the luna’s path and the handler’s name, with the arguments as a JSON object.
curl -X POST "$LUNA_URL/List/groceries/add" \
-H "luna-key: $LUNA_KEY" \
-H "Content-Type: application/json" \
-d '{"text":"milk"}'
Reference
Method and path
POST <LUNA_URL>/<Type>/<KEY_1>/<KEY_2>/<handler>
| Part | Holds |
|---|---|
<LUNA_URL> |
The environment’s URL, as .env.local has it |
<Type> |
The luna type’s name, such as List |
<KEY_n> |
One segment for each key field, in the order the type declares them. A stateless luna type has none |
<handler> |
The handler’s name |
Each segment is percent-decoded once, so a key may hold /, % or + written as %2F, %25 or %2B. A + left as it is stays a +.
| Key field | Segment |
|---|---|
t.string(), t.uuid(), t.enum() |
The value as text |
t.int() |
The number in decimal |
t.bytes() |
URL-safe base64, with or without = padding |
Credentials
| Credential | Send it as | Reaches |
|---|---|---|
| Publishable key | luna-key: <KEY>, or ?key=<KEY> in the address |
The public luna types the key allows |
| Secret token | Authorization: Bearer <TOKEN> |
Every luna type, private ones too |
A private luna type answers a publishable key as if it did not exist. See Keys and tokens.
Request headers
luna-key?string
A publishable key. Not needed with a secret token.
stringauthorization?Bearer <TOKEN>
A secret token.
Bearer <TOKEN>luna-tenant?string
The tenant the call is for, by name, when the URL's host does not say.
stringluna-timeout-ms?integer
How long to wait for the handler, in milliseconds. A streaming call has a default of its own.
integer60000idempotency-key?string
A key of your choosing. A call repeated with the same key runs once and gets the first call's answer.
stringluna-read?string
For a query handler only. latest asks the luna itself. max-stale=<MS> lets a read copy that far behind answer. at-least=<POSITION> needs a copy that has the write at that position.
stringtraceparent?string
A W3C trace context. The call joins that trace.
stringaccept?string
text/event-stream asks a streaming handler for each value as it is yielded.
stringorigin?string
Checked against the web origins the publishable key allows.
stringEvery header and query parameter also reaches the luna type’s onBeforeConnect hook, as request.headers and request.query.
Body
The body is a JSON object of the handler’s arguments. An empty body means {}. Luna checks the arguments against the handler’s types before the luna wakes.
Response
A handler that returns answers 200 with its result as JSON, and Content-Type: application/json. A handler that returns nothing still answers 200.
| Response header | When |
|---|---|
traceparent |
The call reached the luna and answered without streaming. It names the call’s trace |
Access-Control-Allow-Origin, Vary: Origin |
The request’s Origin is one the environment’s publishable keys allow |
Content-Type: text/event-stream, Cache-Control: no-cache |
A streaming handler answering with accept: text/event-stream |
Status codes
| Status | code |
When |
|---|---|---|
| 400 | Invalid |
The wrong number of key segments, a key or argument of the wrong type, a body that is not a JSON object, or a bad luna-read |
| 401 | Unauthorized |
No valid publishable key or secret token |
| 402 | PaymentRequired |
The organisation must pay before calls run |
| 403 | The luna’s own code | onBeforeConnect refused the call with ctx.fail |
| 403 | Unauthorized |
The publishable key does not allow this origin |
| 403 | Suspended |
The organisation is suspended |
| 404 | NotFound |
No such tenant, luna type or handler, a private type called with a publishable key, or a luna that does not exist called on a handler with requires: "exists" |
| 405 | Invalid |
GET or PUT to a handler’s address |
| 409 | Cancelled |
The durable run was cancelled |
| 422 | The handler’s code | A declared error, from ctx.fail |
| 429 | Overloaded |
A publishable key over its rate, a luna with too many calls waiting, or the tenant over its request rate |
| 429 | QuotaExceeded |
The organisation is over a quota of its plan |
| 500 | Internal, OutOfMemory |
The handler threw, returned the wrong type, or ran out of memory |
| 502 | OutcomeUnknown |
The luna stopped after it got the call. It may have run |
| 503 | Unavailable, Quarantined, NoRoom |
The luna cannot run now, and the call never started |
| 504 | Timeout |
The deadline passed before the handler answered |
| 507 | QuotaExceeded |
The tenant or organisation is over its storage, so only query handlers run |
Each code has a page of its own, such as Invalid, NotFound and Timeout.
Error body
Luna’s own errors carry a code and a message.
{"error":{"code":"Unauthorized","message":"a valid publishable key (luna-key header or ?key=) or API token is required"}}
A declared error carries the handler’s code and its data, with status 422. A refusal from onBeforeConnect carries its code and data, with status 403.
error.codestring
Luna's code, the handler's declared code, or the code onBeforeConnect failed with.
stringerror.message?string
What went wrong. Not on declared errors.
stringerror.data?unknown
What ctx.fail sent with a declared error or a refusal.
unknownerror.retryAfterMs?number
On NoRoom: when to try again.
numberGood to know
Overloaded,Timeout,OutcomeUnknownandUnavailableare worth a retry. Send anidempotency-keywhen the handler writes, so the retry runs it once.- Luna keeps a call’s answer under its
idempotency-keyfor 3 days, and only once the luna exists. An answer that is one of Luna’s own errors is not kept, so a retry runs the handler again. - Each publishable key may make 100 calls a second, in bursts of up to 200, unless the key sets its own rate. Calls over it answer
429. Secret tokens have no rate of their own. - A page in a browser on another origin may send every header above.
luna-read: at-least=<POSITION>takes a position fromctx.position()in an earlier write, and sees that write.- A handler that answers with a
Responsebelongs at its own route. Called by name here, it answers200with{"$http": {"status", "headers", "body"}}, its body in base64. See Serve HTTP routes.
Examples
Read a list
curl -X POST "$LUNA_URL/List/groceries/all" -H "luna-key: $LUNA_KEY"
[{"id":1,"text":"milk"}]
Retry a write safely
curl -X POST "$LUNA_URL/Wallet/ada/charge" \
-H "Authorization: Bearer <SECRET_TOKEN>" \
-H "idempotency-key: order-1042" \
-H "luna-timeout-ms: 5000" \
-d '{"amount":500}'
Call a luna with a key of several fields
A Card keyed by { board, card } takes two segments.
curl -X POST "$LUNA_URL/Card/b1/c7/rename" \
-H "luna-key: $LUNA_KEY" \
-d '{"title":"Ship it"}'
Receive a streaming handler’s values
Each value arrives as an SSE data event. A failure after the stream opened arrives as a last event named error, whose data is the error body’s error object.
curl -N -X POST "$LUNA_URL/Room/lobby/count" \
-H "luna-key: $LUNA_KEY" \
-H "accept: text/event-stream" \
-d '{"to":3}'
See Stream a response.
Other addresses of a luna
| Method and path | Does | Page |
|---|---|---|
GET /<Type>/<key...>/connect |
Opens a WebSocket, or a one-way SSE stream with accept: text/event-stream |
Open a connection |
GET /<Type>/<key...>/streams/<name> |
Reads one of the luna’s streams | Follow a stream from an app |
PUT and GET /<Type>/<key...>/blobs/<bucket>/<path> |
Uploads to and downloads from a signed URL | Upload from an app |