Node.js SDK

Official Node.js SDK for Berican Labs API

Installation

Install the SDK using npm:

npm install @berican/sdk

Quick Start

import { Berican } from '@berican/sdk';

// Initialize the client
const client = new Berican({
  apiKey: 'your-api-key-here', // Get from https://bericanlabs.com/dashboard/settings
});

// List all sites
const sites = await client.sites.list();
console.log('My sites:', sites.data);

// Create a new site
const newSite = await client.sites.create({
  name: 'my-awesome-site',
  type: 'STATIC',
  repository: 'github.com/user/repo',
  branch: 'main',
  framework: 'nextjs',
});

// Deploy the site
const deployment = await client.deployments.create(newSite.id);
console.log('Deployment:', deployment);

Authentication

Get your API key from the Berican Labs Dashboard.

const client = new Berican({
  apiKey: process.env.BERICAN_API_KEY,
  baseUrl: 'https://api.bericanlabs.com/v1', // Optional, defaults to production
});

Sites API

List Sites

const response = await client.sites.list({
  page: 1,
  perPage: 20,
});

console.log(response.data); // Array of sites
console.log(response.pagination); // Pagination info

Deployments API

Create a Deployment

// Trigger a new deployment
const deployment = await client.deployments.create('site-id', {
  clearCache: true,
  forceBuild: false,
});

// Wait for completion
while (deployment.status === 'BUILDING' || deployment.status === 'DEPLOYING') {
  await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds
  deployment = await client.deployments.get('site-id', deployment.id);
}

if (deployment.status === 'SUCCESS') {
  console.log('Deployed successfully!', deployment.deployUrl);
} else {
  console.error('Deployment failed:', deployment.logs);
}

Domains API

Check Domain Availability

const availability = await client.domains.checkAvailability('example.co.ke');
console.log(availability.available); // true or false
console.log(availability.price); // KSh 1200

Error Handling

The SDK throws BericanSDKError for API errors:

import { Berican, BericanSDKError } from '@berican/sdk';

try {
  const site = await client.sites.get('invalid-id');
} catch (error) {
  if (error instanceof BericanSDKError) {
    console.error('API Error:', error.message);
    console.error('Status Code:', error.statusCode);
    console.error('Details:', error.bericanError);
  } else {
    console.error('Unexpected error:', error);
  }
}

Examples

Deploy on Git Push
import { Berican } from '@berican/sdk';

const client = new Berican({ apiKey: process.env.BERICAN_API_KEY });

// Webhook handler (Express.js example)
app.post('/webhook/github', async (req, res) => {
  const { repository, ref } = req.body;

  if (ref === 'refs/heads/main') {
    const deployment = await client.deployments.create(process.env.SITE_ID);
    console.log('Deployment triggered:', deployment.id);
  }

  res.sendStatus(200);
});
Monitor Deployment Status
async function deployAndWait(siteId: string) {
  const deployment = await client.deployments.create(siteId);

  console.log('Deployment started:', deployment.id);

  while (true) {
    const status = await client.deployments.get(siteId, deployment.id);

    console.log('Status:', status.status);

    if (status.status === 'SUCCESS') {
      console.log('Deployed to:', status.deployUrl);
      break;
    }

    if (status.status === 'FAILED') {
      console.error('Deployment failed!');
      console.error(status.logs);
      throw new Error('Deployment failed');
    }

    await new Promise(resolve => setTimeout(resolve, 5000));
  }
}