Safaribid Docs

Node.js / TypeScript SDK

Integrate Safaribid into your JavaScript or TypeScript applications.

Node.js / TypeScript SDK

The official Safaribid SDK for Node.js provides a convenient way to access our APIs from server-side JavaScript and TypeScript.

Installation

npm install @safaribid/sdk
# or
yarn add @safaribid/sdk

Initialization

Initialize the client with your secret API key.

import { Safaribid } from '@safaribid/sdk';

const client = new Safaribid({
  apiKey: process.env.SAFARIBID_API_KEY,
  environment: 'production' // defaults to 'sandbox'
});

Usage

Get a Delivery Quote

const quote = await client.delivery.getQuote({
  pickup_address: 'Westlands, Nairobi',
  dropoff_address: 'Kilimani, Nairobi',
  package_type: 'food'
});

console.log(quote.delivery_fee);

Create a Delivery

const delivery = await client.deliveries.create({
  pickup: {
    name: 'Main Store',
    address: 'Central Square, Nairobi',
    phone: '+254700000000'
  },
  dropoff: {
    name: 'Alice Johnson',
    address: 'Apartment 4B, Upper Hill',
    phone: '+254711111111'
  },
  package: {
    type: 'small',
    weight: 0.5
  },
  payment_method: 'wallet'
});

Webhook Verification

The SDK includes a utility for verifying webhook signatures.

import { Webhook } from '@safaribid/sdk';

const isValid = Webhook.verifySignature(
  req.body,
  req.headers['x-safaribid-signature'],
  process.env.SAFARIBID_WEBHOOK_SECRET
);

Error Handling

The SDK throws structured errors that you can catch and handle.

import { SafaribidError } from '@safaribid/sdk';

try {
  await client.deliveries.create({ ... });
} catch (err) {
  if (err instanceof SafaribidError) {
    console.error(`Error Code: ${err.code}, Message: ${err.message}`);
  }
}

On this page