JavaScript SDK
@aurabase/aurabase-js — typed client for Node, Bun, Deno, Next.js, React, SvelteKit, Vue. Covers Database, Auth, Realtime, Storage, Functions.
One package, four managers#
pnpm add @aurabase/aurabase-js
The SDK directly includes @aurabase/aurabase-js/ssr export for Next.js (App Router, Server Actions, Middleware), Remix, Nuxt and SvelteKit with automatic management of session cookies and anti-truncation chunking.
Create the client#
import { createClient } from '@aurabase/aurabase-js'
import type { Database } from './aurabase.types'
export const aura = createClient<Database>(
process.env.NEXT_PUBLIC_AURABASE_URL!,
process.env.NEXT_PUBLIC_AURABASE_ANON_KEY!,
{ auth: { persistSession: true } }
)
Native Server-Side Rendering support#
For SSR architectures (Next.js App Router, Middleware, Server Actions, SvelteKit), import createServerClient from @aurabase/aurabase-js/ssr. The user session is automatically stored in secure httpOnly cookies, with multi-chunk support for large tokens:
import { createServerClient } from '@aurabase/aurabase-js/ssr'
import { NextResponse, type NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
let response = NextResponse.next({ request })
const aura = createServerClient(
process.env.NEXT_PUBLIC_AURABASE_URL!,
process.env.NEXT_PUBLIC_AURABASE_ANON_KEY!,
{
cookies: {
getAll() { return request.cookies.getAll() },
setAll(cookies) {
cookies.forEach(({ name, value, options }) => {
request.cookies.set(name, value)
response.cookies.set(name, value, options)
})
},
},
})
await aura.auth.getSession()
return response
}
Codegen from your schema#
npx @aurabase/gen --out lib/aurabase.types.ts (or aura db generate, which writes types.ts in the current folder) generates the types file from your complete schema (tables, views, enums, RPC functions). To plug into createClient<Database> to type all of your queries.
{
"scripts": {
"types:gen": "npx @aurabase/gen --out src/aurabase.types.ts",
"migrate": "aura db push && pnpm types:gen"
}
}
By service#
Database
.from(table).select(cols?)→SelectBuilder<T>.from(table).insert(row)→Promise<{ data, error }>.from(table).update(patch)→UpdateBuilder<T>.from(table).delete()→DeleteBuilder<T>.rpc(fn, args)→Promise<{ data, error }>.nosql.collection(name)→NoSqlCollectionClient(find(),insertOne(),aggregate(),createIndex())
Auth
.auth.signUp({ email, password, options? })→Promise<AuraResponse<LoginResponse>>.auth.signInWithPassword({ email, password })→Promise<AuraResponse<LoginResponse>>.auth.signInWithOAuth(provider, opts?) | ({ provider, ... })→Promise<AuraResponse<{ url }>>.auth.getSession()→Promise<{ data: { session }, error }>.auth.signOut()→Promise<AuraResponse<void>>.auth.mfa.enroll() | .auth.mfaEnroll()→Promise<AuraResponse<MfaEnrollResponse>>
Realtime
.channel(name, config?)→Channelchannel.on(event, filter, cb)→Channelchannel.subscribe()→Channelchannel.send({ type, event, payload })→Promise<void>channel.unsubscribe()→Promise<void>
Storage
.storage.from(bucket).upload(path, file, opts?)→Promise<{ data, error }>.storage.from(bucket).download(path)→Promise<Blob>.storage.from(bucket).getPublicUrl(path, opts?)→{ publicUrl }.storage.from(bucket).createSignedUrl(path, expiresIn)→Promise<{ signedUrl }>.storage.from(bucket).remove(paths[])→Promise<{ data, error }>
Functions
.functions.invoke(name, { body?, method?, path?, headers?, responseType?, signal?, timeout? })→Promise<{ data, error }>.functions.list()→Promise<{ data: FunctionDef[], error }>.functions.get(nameOrId)→Promise<{ data: FunctionDef, error }>.functions.listJobs({ status?, limit? })→Promise<{ data: Job[], error }>.functions.getJob(jobId)→Promise<{ data: Job, error }>.functions.enqueueJob(functionName, { payload?, delaySecs?, maxAttempts? })→Promise<{ data: Job, error }>.functions.listCronJobs()→Promise<{ data: CronJob[], error }>.functions.createCronJob(name, schedule, functionName, { payload? })→Promise<{ data: CronJob, error }>.functions.deleteCronJob(name)→Promise<{ data, error }>
Only invoke is open to the public (anon) key. list, get, listJobs, getJob, enqueueJob, listCronJobs, createCronJob and deleteCronJob require a service_role key and belong on your server: a FunctionDef carries the function's source code and environment variables, so these calls answer 403 to a public key. invoke reports errors as FunctionsHttpError (the function answered with an error, any status) or FunctionsRelayError (the platform failed: network, 502, 503, 504), both exported by @aurabase/aurabase-js.
Uniform shape { data, error }#
Each async method returns { data, error }. error is null on success, otherwise an AuraError instance (also exported under the alias AurabaseError) with code, message, status, and details. Pass { parseBigIntAsString: true } in createClient options if your schema uses 64-bit bigint values beyond Number.MAX_SAFE_INTEGER.
import { AurabaseError } from '@aurabase/aurabase-js'
const { data, error } = await aura.from('users').select('*').single()
if (error) {
if (error.code === 'PGRST116') notFound()
throw error // instanceof AuraError / AurabaseError
}