JavaScript / TypeScript SDK
The official CocoBase JS SDK — works in browsers, Node.js, Next.js, SvelteKit, Nuxt, React Native, and any JS runtime. Package:cocobase · Version: 1.6.x · npm: npmjs.com/package/cocobase
Installation
- npm
- yarn
- pnpm
- Browser (CDN)
Initialization
SSR / Server-side (Next.js, Node.js)
By default the SDK auto-detects the environment and uses in-memory storage whenlocalStorage is not available. For explicit control:
CocobaseConfig options
Documents — CRUD
listDocuments<T>(collection, query?)
getDocument<T>(collection, id)
createDocument<T>(collection, data)
updateDocument<T>(collection, id, data)
Partial update — only fields you pass are changed. Other fields are preserved.
replaceDocument<T>(collection, id, data)
Full replace — sends all fields, replaces the document entirely.
deleteDocument(collection, id)
Batch Operations
Querying
Filter operators
Sorting & pagination
Count & aggregate
File + document combined
Live Queries — onSnapshot
Subscribe to a collection and get called immediately with current data, then again whenever anything changes (create/update/delete). Returns an unsubscribe function to call on cleanup.
Pagination Helper — paginate
Stateful pagination — tracks page position, detects hasMore, no backend changes needed.
Authentication
Register / Login / Logout
Restore session on app load — initAuth
Call once on startup to restore a saved session. Safe on servers — network errors and 5xx responses do not log the user out (only explicit 401/403 from the server do).
onAuthStateChange — reactive auth
Returns an unsubscribe function. Works like Firebase’s onAuthStateChanged.
onAuthEvent — all auth lifecycle events
OAuth
Two-Factor Authentication (2FA)
Other auth methods
Token helpers
Real-time
Watch a collection
Project broadcast (pub/sub)
Room chat
List rooms
Cloud Functions
Typed Errors
All API errors throw a typed subclass ofCocobaseError. Use instanceof to branch:
Storage Adapters
TypeScript Types
Key types exported fromcocobase:
Next Steps
- Flutter SDK — Dart/Flutter equivalent
- MCP Server — AI assistant integration
- Authentication guide
- Querying guide
- Real-time guide
