Write from the action
Add an action named create_post with this code.
Action
async ({ input, orbi }) => {
const apiKey = orbi.project.apiKeys.find((key) => key.connectorId)
if (!apiKey) throw new Error("Create an API key for the database under Workspace settings, API Keys.")
const response = await fetch("https://core.orbitype.com/api/sql/v1", {
method: "POST",
headers: {
"content-type": "application/json",
"X-API-KEY": apiKey.key,
},
body: JSON.stringify({
sql: "INSERT INTO posts (title) VALUES (:title) RETURNING *",
bindings: { title: input.title },
}),
})
if (!response.ok) throw new Error(await response.text())
return await response.json()
}Call it from the page
In the document, call the action and show the title it returns.
const created = await Orbi.action("create_post", { title: "Hello" })
const row = created.output?.[0]
document.body.textContent = row ? row.title : "No row"Reload the preview. The page shows Hello. Open the posts table. The row is there.
The same INSERT works from outside the app once you send the X-API-KEY header. That page is SQL API.
If this fails: The page says No row
output is the array of rows. If it is empty, the insert did not return a row. Add RETURNING *. If the action name does not match, you get action not found. If the table name is wrong, you get relation does not exist.