# Aurabase JavaScript / TypeScript SDK Reference (@aurabase/aurabase-js) > NPM package: @aurabase/aurabase-js (pnpm monorepo; sub-packages @aurabase/core, @aurabase/auth, @aurabase/db, @aurabase/storage, @aurabase/realtime, @aurabase/functions, @aurabase/ai, @aurabase/notifications) > Documentation: https://aurabase.dev/en/docs/sdk-js > Supported runtimes: Node.js 20+, Bun, Deno, Modern Browsers, Cloudflare Workers > SSR support via `@aurabase/aurabase-js/ssr` (createBrowserClient / createServerClient) > Next.js support via `@aurabase/aurabase-js/nextjs` (createAuthMiddleware, createClientForServerComponent, createClientForServerAction) > React support via `@aurabase/aurabase-js/react` (AuthProvider, useAuth, useUser, useSession, SignedIn, SignedOut) ## Installation ```bash npm install @aurabase/aurabase-js # or install specific tree-shakable packages: npm install @aurabase/db @aurabase/auth ``` ## Initialization ```typescript import { createClient, createAdminClient } from '@aurabase/aurabase-js' import type { Database } from './aurabase.types' // generated via 'aura db generate' // 1. Standard Client (Browser / Client-Side) export const aura = createClient( process.env.NEXT_PUBLIC_AURABASE_URL!, // https://.aurabase.dev process.env.NEXT_PUBLIC_AURABASE_ANON_KEY!, // aura_anon_xxx { auth: { persistSession: true, autoRefresh: true, flowType: 'pkce' }, retry: { enabled: true, maxRetries: 3 }, requestTimeoutMs: 30000, } ) // 2. Admin Client (Server-Side Only with service_role key) // SECURITY WARNING: NEVER expose service_role in client-side bundles (throws a security exception) export const admin = createAdminClient( process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY! // aura_sk_xxx ) ``` ## Unified Response Shape (`AuraResponse`) Every SDK call returns a discriminated union without throwing unhandled exceptions: ```typescript type AuraResponse = | { data: T; error: null; meta?: Meta } // Success with payload | { data: null; error: null; meta?: Meta } // Success 204 (no content) | { data: null; error: AuraError; meta?: Meta } // HTTP / Business error // Built-in AuraError helpers: // error.isNotFound, error.isUnauthorized, error.isForbidden, error.isRateLimited ``` ## Database Queries (PostgreSQL 18) ```typescript // SELECT with filters, sorting, and pagination const { data: posts, error } = await aura.db .from('posts') .select('id, title, content, published, created_at') .eq('published', true) .ilike('title', '%rust%') .order('created_at', { ascending: false }) .range(0, 19) // limit/offset pagination // Modifiers const { data: singlePost } = await aura.from('posts').select('*').eq('id', 'uuid-1').single() const { count } = await aura.from('posts').select('*', { count: 'exact', head: true }) // Available filters: // .eq(), .neq(), .gt(), .gte(), .lt(), .lte(), .like(), .ilike(), .is(), .in(), .or(), .not() // Mutations await aura.from('posts').insert({ title: 'New Post', published: true }) await aura.from('posts').upsert({ id: 'uuid-1', title: 'Updated' }, { onConflict: 'id' }) await aura.from('posts').update({ published: false }).eq('id', 'uuid-1') await aura.from('posts').delete().eq('id', 'uuid-1') // Stored Procedure RPC const { data: total } = await aura.db.rpc('calculate_user_balance', { user_id: 'usr_123' }) ``` ## NoSQL Document Store (MongoDB Compatible) ```typescript // Insert BSON/JSON document const { data: doc } = await aura.nosql.collection('events').insertOne({ type: 'page_view', path: '/pricing', timestamp: new Date() }) // Query with MongoDB operators ($gte, $in, regex) const { data: events } = await aura.nosql.collection('events') .find({ type: { $in: ['page_view', 'click'] } }) .sort({ timestamp: -1 }) .limit(50) // Multi-stage Aggregation Pipeline const { data: stats } = await aura.nosql.aggregate([ { $match: { type: 'conversion' } }, { $group: { _id: '$campaign', total: { $sum: '$amount' } } }, { $sort: { total: -1 } } ]) ``` ## Authentication 2.1 & Passkeys ```typescript // Sign up / Sign in with password await aura.auth.signUp({ email: 'dev@company.eu', password: 'SuperPassword123!' }) await aura.auth.signInWithPassword({ email: 'dev@company.eu', password: 'SuperPassword123!' }) // OAuth 2.1 (15 providers: google, github, discord, apple, microsoft, twitter, etc.) await aura.auth.signInWithOAuth('github', { redirectTo: 'https://myapp.com/auth/callback' }) // Passwordless Magic Links & OTP await aura.auth.signInWithMagicLink({ email: 'dev@company.eu' }) await aura.auth.signInWithEmailOtp('dev@company.eu') await aura.auth.verifyOtp({ type: 'email', email: 'dev@company.eu', token: '123456' }) await aura.auth.signInWithSmsOtp('+33612345678') await aura.auth.verifyOtp({ type: 'sms', phone: '+33612345678', token: '123456' }) // Anonymous Authentication await aura.auth.signInAnonymously() // TOTP MFA & Passkeys WebAuthn const enrollment = await aura.auth.mfaEnroll() await aura.auth.mfaVerifySetup('123456') await aura.auth.mfaVerify({ mfa_token: 'token', code: '123456' }) // Session management const { data: session } = await aura.auth.getSession() const { data: user } = await aura.auth.getUser() await aura.auth.signOut() ``` ## Sovereign S3 Storage ```typescript // Upload (Blob, File, Buffer, Uint8Array) await aura.storage.from('avatars').upload('users/1.png', fileBuffer, { contentType: 'image/png', upsert: true }) // Temporary signed URLs const { data: signed } = await aura.storage.from('documents').createSignedUrl('contract.pdf', 3600) // Public URL with on-the-fly image transformations const publicUrl = aura.storage.from('avatars').getPublicUrl('users/1.png', { transform: { width: 400, height: 400, resize: 'cover', format: 'webp', quality: 85 } }) // List & delete const { data: files } = await aura.storage.from('avatars').list('users/') await aura.storage.from('avatars').remove(['users/1.png']) ``` ## Realtime (WebSockets & CDC) ```typescript const channel = aura.realtime.channel('chat-room-1') // PostgreSQL Change Data Capture (CDC) channel.on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages', filter: 'room_id=eq.chat-room-1' }, (payload) => { console.log('New message:', payload.new) }) // Ephemeral Broadcast channel.on('broadcast', { event: 'typing' }, ({ payload }) => { console.log(`${payload.user} is typing...`) }) // Collaborative Presence channel.on('presence', { event: 'sync' }, () => { console.log('Online users:', channel.presenceState()) }) channel.subscribe() await channel.track({ user: 'Alice', online_at: new Date().toISOString() }) await channel.send({ type: 'broadcast', event: 'typing', payload: { user: 'Alice' } }) ``` ## Edge Functions & AI Gateway ```typescript // Edge Functions invocation const { data, error } = await aura.functions.invoke('process-payment', { method: 'POST', body: { amount: 4900, currency: 'EUR' } }) // Unified Multi-Provider AI Chat const { data: answer } = await aura.ai.chat([ { role: 'user', content: 'Explain PostgreSQL CDC architecture.' } ], { provider: 'anthropic', model: 'claude-3-5-sonnet-20241022', temperature: 0.2 }) // Vector Embeddings, Ingestion & Semantic Search const { data: embedResult } = await aura.ai.embed({ text: 'Sovereign EU cloud infrastructure', namespace: 'documentation' }) const { data: matches } = await aura.ai.semanticSearch('How to configure RLS?', 'documentation', { top_k: 5 }) const { data: ragResult } = await aura.ai.rag({ namespace: 'documentation', question: 'What are the default roles?' }) const { data: sqlQuery } = await aura.ai.nl2sql('How many active users this month?', { limit: 10 }) ```