TypeScript SDK
The official TypeScript SDK for Butterbase. Works in browser, Node.js, and Deno environments.
Installation
Section titled “Installation”npm install @butterbase/sdkQuick start
Section titled “Quick start”import { createClient } from '@butterbase/sdk';
const butterbase = createClient({ appId: 'app_abc123', apiUrl: 'https://api.butterbase.ai', anonKey: 'your-anon-key' // Optional, for public access});Data operations
Section titled “Data operations”const { data, error } = await butterbase .from('posts') .select('*') .eq('status', 'published') .order('created_at', { ascending: false }) .limit(10);Insert
Section titled “Insert”const { data, error } = await butterbase .from('posts') .insert({ title: 'Hello World', content: 'My first post' });Update
Section titled “Update”const { data, error } = await butterbase .from('posts') .update({ status: 'archived' }) .eq('id', '123');Delete
Section titled “Delete”const { data, error } = await butterbase .from('posts') .delete() .eq('id', '123');Query operators
Section titled “Query operators”| Method | SQL Equivalent |
|---|---|
eq(column, value) | = |
neq(column, value) | != |
gt(column, value) | > |
gte(column, value) | >= |
lt(column, value) | < |
lte(column, value) | <= |
like(column, pattern) | LIKE (case-sensitive) |
ilike(column, pattern) | ILIKE (case-insensitive) |
in(column, values) | IN (...) |
is(column, value) | IS NULL / TRUE / FALSE |
Query modifiers
Section titled “Query modifiers”| Method | Description |
|---|---|
select(columns) | Select specific columns |
order(column, options) | Order results |
limit(count) | Limit results |
offset(count) | Skip results |
Authentication
Section titled “Authentication”Sessions are automatically persisted to localStorage and restored on page refresh. Access tokens are automatically refreshed before they expire.
// Sign upconst { data, error } = await butterbase.auth.signUp({ email: 'user@example.com', password: 'secure123'});
// Sign inconst { data, error } = await butterbase.auth.signIn({ email: 'user@example.com', password: 'secure123'});
// Get current userconst { data: user } = await butterbase.auth.getUser();
// Sign outawait butterbase.auth.signOut();
// Refresh session manuallyconst { data } = await butterbase.auth.refreshSession();
// OAuthconst { url } = butterbase.auth.signInWithOAuth({ provider: 'google', redirectTo: 'http://localhost:3000/callback'});window.location.href = url;Auth state changes
Section titled “Auth state changes”const { unsubscribe } = butterbase.onAuthStateChange((event, session) => { // event: 'SIGNED_IN' | 'SIGNED_OUT' | 'TOKEN_REFRESHED' | 'SESSION_RESTORED' console.log(event, session?.user);});Custom session storage
Section titled “Custom session storage”// Disable persistenceconst butterbase = createClient({ appId: 'app_abc123', apiUrl: 'https://api.butterbase.ai', persistSession: false,});
// Custom adapter (e.g. React Native)const butterbase = createClient({ appId: 'app_abc123', apiUrl: 'https://api.butterbase.ai', sessionStorage: myCustomStorage, // implements { getItem, setItem, removeItem }});Storage
Section titled “Storage”const { data, error } = await butterbase.storage.upload(file);const { data } = await butterbase.storage.getDownloadUrl(objectId);const { data: objects } = await butterbase.storage.list();await butterbase.storage.delete(objectId);Functions
Section titled “Functions”const { data, error } = await butterbase.functions.invoke('my-function', { body: { key: 'value' }, method: 'POST'});TypeScript support
Section titled “TypeScript support”All methods return ButterbaseResponse<T> with proper type inference:
interface Post { id: string; title: string; content: string; status: 'draft' | 'published';}
const { data, error } = await butterbase .from<Post>('posts') .select('*') .eq('status', 'published');// data is typed as Post[] | nullDeno usage
Section titled “Deno usage”import { createClient } from 'npm:@butterbase/sdk';
const butterbase = createClient({ appId: Deno.env.get('BUTTERBASE_APP_ID')!, apiUrl: Deno.env.get('BUTTERBASE_API_URL')!,});Migration from 0.x to 1.0
Section titled “Migration from 0.x to 1.0”The authUrl parameter has been removed. All auth endpoints now run on the same URL as the API — just use apiUrl.