Code and SDKs
Write server functions
Put trusted logic, secrets and calls to other services in server functions your app's screens call by name.
Server functions run on Ahamo's servers, not in the browser. Use them for anything the browser must not be trusted
with, such as permission checks, secrets, prices, calls to other services. The build AI writes them in the app's API code
with @ahamo/runtime; screens call them by name.
When to use it
- An action needs a check the browser could skip, such as "only the owner may cancel".
- The code needs a secret key or calls another service.
How to use it
Ask the build AI, or write one yourself:
// Server code (apps/api). Runs on Ahamo's servers; secrets are safe here.
export const functions = defineFunctions({
cancelBooking: {
access: 'signedIn',
async run({ input, actor, db }) {
const booking = await db.table('bookings').get(input.id)
if (booking.ownerId !== actor.id) throw new FunctionError('Only the owner can cancel this booking.')
return db.table('bookings').update(input.id, { status: 'cancelled' })
},
},
})| Need | Use |
|---|---|
| A secret or setting | env('NAME') (secrets and variables) |
| A connected account, such as Slack or Google | connectors.fetch(alias, path) (external APIs) |
| Work later or on a schedule | defineTasks and runLater from @ahamo/tasks/server |
| AI | @ahamo/ai |
| Notifications | @ahamo/push/server (notifications) |
A function is for signed-in people by default; mark it access: 'public' only when anyone may call it.
Plans and limits
| Rule | Value |
|---|---|
| Time for one call | Keep it under 10 seconds on every plan; Team allows longer calls |
| Who may call | Signed-in people, unless the function is marked public |
| Secrets | Readable only here, never in browser code |
Move work that takes longer, or runs on a schedule, to a task.
Troubleshooting
"function-failed" in the browser. The function threw a FunctionError; its message is meant for the person.
Other errors are reported without details; read them in the app's logs.
A function stops partway on a long job. It hit the time limit. Move the work to a task with runLater.