Your first Edge Function
You will write a function, deploy it to your project, call it from Node.js and read its result. Every command below was run for real.
Before you start#
You need:
- Node.js 20 or later.
- The
auraCLI. Install it withnpm install -g @aurabase/cli(see the CLI reference for other platforms). - An Aurabase account.
1. Create and link a project#
Sign in, create a project, and link your working folder to it. Replace <org-slug> with the slug shown by aura orgs list.
aura login --email [email protected] --password '<your-password>'
aura orgs list
aura projects create my-app --org <org-slug>The last command prints the project URL and your first API keys. Copy them now: the initial keys are shown only once. If you miss them, create new ones in the Studio under Settings, API keys.
✔ Project created: my-app (e99f2c4f-43e2-4d08-b023-b4f9e687d17c)
import { createClient } from '@aurabase/aurabase-js'
const client = createClient('http://my-app-ajap5iw2.localhost:8080', 'aura_anon_…')Link the current folder to the project:
aura link --project-id e99f2c4f-43e2-4d08-b023-b4f9e687d17c ✔ Project e99f2c4f-43e2-4d08-b023-b4f9e687d17c linked in aurabase.toml.
• Project URL: http://my-app-ajap5iw2.localhost:8080Then tell the CLI where the data plane is and which key to use. A service_role key is required to deploy, because deploying code is an administrative action.
NEXT_PUBLIC_AURABASE_URL=http://my-app-ajap5iw2.localhost:8080
AURABASE_SERVICE_ROLE_KEY=<your service_role key>A service_role key bypasses row-level security and can deploy code. Never put it in a browser bundle or commit .env.
2. Write the function#
aura functions new hello✔ New function created in aurabase/functions/hello (TypeScript Deno template)Open aurabase/functions/hello/index.ts and replace its content:
Deno.serve(async (req: Request) => {
const { name = 'world' } = await req.json().catch(() => ({}))
return new Response(JSON.stringify({ greeting: `Hello, ${name}!` }), {
headers: { 'content-type': 'application/json' },
})
})A function must call Deno.serve. An export default is never called.
3. Deploy it#
aura functions deploy hello ✔ Detected TypeScript script (runtime: deno)...
• Uploading to service (runtime: deno)...
✔ Function 'hello' deployed successfully!Call it once from the CLI to check that it answers:
aura functions invoke hello --data '{"name":"Ada"}' ✔ Response (200):
{
"data": {
"body": { "greeting": "Hello, Ada!" },
"duration_ms": 74,
…4. Call it from the SDK#
mkdir hello-client && cd hello-client
npm init -y
npm install @aurabase/aurabase-jsimport { createClient } from '@aurabase/aurabase-js'
const aura = createClient(process.env.NEXT_PUBLIC_AURABASE_URL, process.env.NEXT_PUBLIC_AURABASE_ANON_KEY)
const { data, error } = await aura.functions.invoke('hello', { body: { name: 'Ada' } })
if (error) throw error
console.log(data.greeting)NEXT_PUBLIC_AURABASE_URL=http://my-app-ajap5iw2.localhost:8080 NEXT_PUBLIC_AURABASE_ANON_KEY=aura_anon_… node hello.mjsHello, Ada!You called a function with the public anon key. That is the only call a public key can make: see who can call what.