Read a tenant's API
Fetch the OpenAPI document Luna writes for each tenant from its deployed code, and know what it describes.
An agent, a code generator or a partner’s developer needs to know which handlers your app has and what each one takes. Luna writes that down for every tenant as an OpenAPI document, made from the code deployed to it.
Fetch the document
Ask the control API for the tenant’s document with a token that holds data:read.
curl https://api.luna.gdrl.dev/_luna/tenants/<TENANT>/openapi.json \
-H "Authorization: Bearer $LUNA_TOKEN"
<TENANT> is the tenant’s full name. Run luna tenants list --json to read it, for the default tenant as well as the ones you added. A name with no tenant answers 404.
Luna makes the document from the deployed version each time you ask, so it always matches what runs. Asking wakes no luna and costs no compute.
Know what the document holds
The document is OpenAPI 3.1. Its title is the tenant’s name followed by API, its version is the deployed version, and its server is the tenant’s own URL.
| Part | Holds |
|---|---|
A POST for each handler |
/<Type>/{key}/<handler>, with one path parameter for each key field, the arguments and the return type |
A GET for each stream |
Reading the stream’s entries from an offset, with live for waiting |
A PUT and a GET for each bucket |
Uploading and downloading objects, with the signed URLs a handler makes |
| An operation for each HTTP route | The route’s method and path, on the tenant’s route host |
Each handler’s operation is named <Type>.<handler>, and its declared errors appear as 422 responses. Handlers whose names start with $ are Luna’s own, and the document leaves them out.
A tenant with nothing deployed gets a document with no paths.
Know which credential each operation takes
A public luna type takes a publishable key in the luna-key header, or a secret token as Authorization: Bearer. A private type takes a secret token only.
The document also lists the headers every call takes: luna-timeout-ms, idempotency-key for a write, and luna-read for a query. Call lunas over HTTP explains each one.
Live connections are WebSockets, which OpenAPI cannot describe. The document explains them in words, and names the messages each type pushes.
Describe your handlers
The text in the document comes from your code. Give a handler a description, and a key field or an argument one with .describe(). Give a handler returns so that its result has a schema.
import { defineLuna, t, table } from "luna";
export const List = defineLuna({
name: "List",
visibility: "public",
key: { id: t.string().describe("The list's id.") },
schema: {
items: table({ id: t.id(), text: t.string() }),
},
handlers: (h) => ({
add: h(
{ description: "Adds an item to the list.", args: { text: t.string().describe("What to add.") } },
(ctx, { text }) => {
ctx.db.items.insert({ text });
},
),
count: h({ query: true, returns: t.int() }, (ctx) => ctx.db.items.count()),
}),
});
The descriptions reach the document with the next deploy.
Use the document
- Give it to a coding agent, so that it calls your lunas with the right arguments.
- Generate a client in a language Luna has no SDK for, with an OpenAPI generator.
- Load it into an API client to try calls by hand.
The control API has a document of its own at /_luna/openapi.json, which needs no credential.
Next steps
- Call lunas over HTTP shows the requests the document describes.
- Automate with API tokens makes a token with
data:read. - Generate a typed client writes Swift types without OpenAPI.