Installation & Setup
Learn how to install and configure the Cocobase JavaScript SDK in your project.
Prerequisites
- Node.js 14.x or higher
- npm, yarn, or pnpm package manager
Installation
Choose your preferred package manager:
npm
npm install cocobase
Yarn
yarn add cocobase
pnpm
pnpm add cocobase
Configuration
1. Get Your API Key
- Visit cocobase.buzz
- Sign in to your account
- Navigate to your project settings
- Copy your API key
2. Initialize the Client
import { Cocobase } from "cocobase";
const db = new Cocobase({
apiKey: "your-api-key-here",
});
3. Environment Variables (Recommended)
For security, store your API key in environment variables:
.env
COCOBASE_API_KEY=your-api-key-here
Usage:
import { Cocobase } from "cocobase";
const db = new Cocobase({
apiKey: process.env.COCOBASE_API_KEY,
});
Framework-Specific Setup
React / Next.js
// lib/cocobase.ts
import { Cocobase } from "cocobase";
export const db = new Cocobase({
apiKey: process.env.NEXT_PUBLIC_COCOBASE_API_KEY,
});
Vue / Nuxt
// plugins/cocobase.ts
import { Cocobase } from "cocobase";
export default defineNuxtPlugin(() => {
const db = new Cocobase({
apiKey: process.env.NUXT_PUBLIC_COCOBASE_API_KEY,
});
return {
provide: {
db,
},
};
});
Node.js / Express
// config/database.js
import { Cocobase } from "cocobase";
export const db = new Cocobase({
apiKey: process.env.COCOBASE_API_KEY,
});
Verify Installation
Test your setup with a simple operation:
async function testConnection() {
try {
// Try to authenticate (if you have credentials)
await db.login("test@example.com", "password");
if (await db.isAuthenticated()) {
console.log("✅ Connected to Cocobase!");
console.log("User:", db.user.email);
}
} catch (error) {
console.error("❌ Connection failed:", error);
}
}
testConnection();
Or test with a simple document operation:
async function testConnection() {
try {
// Create a test document
const result = await db.createDocument("test_collection", {
message: "Hello, Cocobase!",
timestamp: new Date().toISOString(),
});
console.log("✅ Connected to Cocobase!");
console.log("Created document:", result);
} catch (error) {
console.error("❌ Connection failed:", error);
}
}
testConnection();
Next Steps
Now that you have Cocobase installed and configured:
- Authentication - Set up user authentication
- CRUD Operations - Start working with data
- Query Builder - Learn advanced querying