React SDK (@nself/client)
@nself/client
Section titled “@nself/client”The React SDK provides a typed client for every nself service surface. It works in any JavaScript environment: React, Vue, Svelte, Node.js, or plain TypeScript.
Install
Section titled “Install”pnpm add @nself/clientCreate a client
Section titled “Create a client”import { createClient } from '@nself/client'
export const nself = createClient({ url: process.env.NEXT_PUBLIC_NSELF_URL!, anonKey: process.env.NEXT_PUBLIC_NSELF_ANON_KEY!,})Set these environment variables in .env.local:
NEXT_PUBLIC_NSELF_URL=https://api.nself.orgNEXT_PUBLIC_NSELF_ANON_KEY=your_anon_key// Sign inconst { session, error } = await nself.auth.signIn({ email, password })
// Sign upawait nself.auth.signUp({ email, password })
// Sign outawait nself.auth.signOut()
// Get current userconst { user } = await nself.auth.getUser()
// Listen to auth changesconst unsub = nself.auth.onAuthStateChange((session) => { console.log('session changed:', session)})unsub() // call to unsubscribeGraphQL
Section titled “GraphQL”Run nself generate --lang typescript to emit typed documents. Then:
import { GetMessagesDocument } from '@/graphql/graphql'
const { data, errors } = await nself.graphql.query(GetMessagesDocument, { variables: { limit: 20 },})
// Mutationawait nself.graphql.mutate(CreateMessageDocument, { variables: { body: 'Hello' },})
// Subscription (realtime via WebSocket)const handle = nself.graphql.subscribe(OnMessageInsertDocument, { onData: ({ data }) => console.log(data), onError: (err) => console.error(err),})handle.unsubscribe()Storage
Section titled “Storage”// Uploadconst { url, error } = await nself.storage.from('avatars').upload('user/photo.jpg', file)
// Downloadconst { blob } = await nself.storage.from('avatars').download('user/photo.jpg')
// Public URLconst publicUrl = nself.storage.from('avatars').getPublicUrl('user/photo.jpg')
// Signed URL (time-limited)const { url } = await nself.storage.from('docs').createSignedUrl('private/report.pdf', 3600)
// Listconst { items } = await nself.storage.from('uploads').list('user-1/')
// Deleteawait nself.storage.from('uploads').remove(['user-1/old-photo.jpg'])Realtime
Section titled “Realtime”// Subscribe to INSERT events on the messages tableconst channel = nself.realtime .channel('inbox') .on('INSERT', (payload) => { console.log('new message:', payload.new) }) .subscribe((status) => { console.log('status:', status) // SUBSCRIBING → SUBSCRIBED })
// Subscribe to all events (INSERT, UPDATE, DELETE)nself.realtime.channel('all-events').on('*', handleAny).subscribe()
// Cleanup (e.g. in React useEffect return)channel.unsubscribe()nself.realtime.removeAllChannels()