Skip to main content

Getting Started with Cocobase

Welcome to Cocobase! This guide will help you set up and make your first API call in just a few minutes.

Prerequisites

Before you begin, make sure you have:
  1. A Cocobase account - Sign up for free
  2. An API key from your project dashboard
  3. Your development environment set up for your preferred language

Installation

Choose your language and install the Cocobase SDK:
npm
npm install cocobase
yarn
yarn add cocobase
pnpm
pnpm add cocobase

Configuration

Initialize Cocobase with your API key:
import { Cocobase } from "cocobase";

const db = new Cocobase({
  apiKey: "YOUR_API_KEY",
});
For Next.js projects:
// lib/cocobase.ts
import { Cocobase } from "cocobase";

export const db = new Cocobase({
  apiKey: process.env.NEXT_PUBLIC_COCOBASE_API_KEY,
});
For React projects:
// src/lib/cocobase.ts
import { Cocobase } from "cocobase";

export const db = new Cocobase({
  apiKey: import.meta.env.VITE_COCOBASE_API_KEY,
});

Your First Request

Let’s create a document in a collection called “users”:
// Create a new user
const user = await db.createDocument("users", {
  name: "Alice Johnson",
  email: "[email protected]",
  role: "developer",
});

console.log("Created user:", user);
Response:
{
  "id": "507f1f77bcf86cd799439011",
  "name": "Alice Johnson",
  "email": "[email protected]",
  "role": "developer",
  "createdAt": "2024-01-15T10:30:00Z",
  "updatedAt": "2024-01-15T10:30:00Z"
}

Reading Data

Now let’s retrieve the document we just created:
// Get all users
const users = await db.listDocuments("users");
console.log("All users:", users);

// Get a specific user by ID
const user = await db.getDocument("users", "507f1f77bcf86cd799439011");
console.log("User:", user);

Environment Variables

For security, always use environment variables for your API key:
Create a .env.local file:
NEXT_PUBLIC_COCOBASE_API_KEY=your_api_key_here
# or for Vite/React
VITE_COCOBASE_API_KEY=your_api_key_here
Never commit your .env files to version control. Add them to .gitignore: bash .env .env.local .env*.local

Next Steps

Now that you’ve made your first request, explore these features:

Common Issues

Make sure your API key is correct and active. You can find it in your Cocobase dashboard.
Add your domain to the allowed origins in your Cocobase project settings.
Make sure you’re using TypeScript 4.5+ and have @types/node installed.

Need Help?