CEND / Developers / Libraries

JavaScript · PHP

Libraries.

Ready-made code so you do not write the requests yourself. Price, book, track and check webhooks in a few lines.

Which one

  • JavaScript (Node 18 or newer). Download cend-js.tgz. Types for TypeScript are included.
  • PHP (8.1 or newer, with curl). Download cend-php.zip.
  • Python. Coming soon. Until then, the API description file works with any code generator.

Both libraries have no other packages to install. They call the same API the Booking API page describes, add a safe-retry key to every booking for you, and check webhook signatures. We will list them on npm and Packagist, and this page will say so when that happens.

JavaScript

# 1. Download cend-js.tgz, put it in your project, then:
npm install ./cend-js.tgz
import { Cend, CendError, verifyWebhook } from '@cend/sdk';

const cend = new Cend(process.env.CEND_SECRET_KEY);

const route = {
    origin_country: 'NG', origin_state: 'Lagos', origin_address: '12 Allen Ave, Ikeja', origin_lat: 6.6018, origin_lng: 3.3515,
    destination_country: 'NG', destination_state: 'Lagos', destination_address: '2 Lekki Road, Lekki', destination_lat: 6.45, destination_lng: 3.5,
    weight_grams: 2000,
};

try {
    const { data: quote } = await cend.quote(route);
    console.log('Delivery costs', quote.total_formatted);

    const booked = await cend.shipments.book(
        { ...route, quote_token: quote.quote_token, /* pickup_*, dropoff_*, item_* ... see the Booking API page */ },
        'order-1001-book', // your own key, so a retry never books twice
    );
    console.log(booked.data.reference, booked.data.tracking_url);
} catch (error) {
    if (error instanceof CendError) console.error(error.status, error.message, error.errors);
    else throw error;
}

// In your webhook handler, with the RAW body:
// const event = verifyWebhook(rawBody, req.headers['cend-signature'], process.env.CEND_WEBHOOK_SECRET);

PHP

# 1. Download cend-php.zip and unzip it into your project (for example libs/cend-php), then:
require __DIR__ . '/libs/cend-php/src/CendException.php';
require __DIR__ . '/libs/cend-php/src/Client.php';

# or with Composer:
composer config repositories.cend path libs/cend-php
composer require cend/cend-php:@dev
<?php
use Cend\CendException;
use Cend\Client;

$cend = new Client(getenv('CEND_SECRET_KEY'));

$route = [
    'origin_country' => 'NG', 'origin_state' => 'Lagos', 'origin_address' => '12 Allen Ave, Ikeja', 'origin_lat' => 6.6018, 'origin_lng' => 3.3515,
    'destination_country' => 'NG', 'destination_state' => 'Lagos', 'destination_address' => '2 Lekki Road, Lekki', 'destination_lat' => 6.45, 'destination_lng' => 3.5,
    'weight_grams' => 2000,
];

try {
    $quote = $cend->quote($route)['data'];
    echo 'Delivery costs ' . $quote['total_formatted'];

    $booked = $cend->bookShipment(
        $route + ['quote_token' => $quote['quote_token'] /* , pickup_*, dropoff_*, item_* ... see the Booking API page */],
        'order-1001-book' // your own key, so a retry never books twice
    );
    echo $booked['data']['reference'], ' ', $booked['data']['tracking_url'];
} catch (CendException $e) {
    error_log($e->status . ' ' . $e->getMessage() . ' ' . json_encode($e->errors));
}

// In your webhook handler, with the RAW body:
// $event = Client::verifyWebhook(file_get_contents('php://input'), $_SERVER['HTTP_CEND_SIGNATURE'] ?? '', getenv('CEND_WEBHOOK_SECRET'));

What they can do

  • Prices and tracking: quote, speeds, options, places and track. Tracking needs no key.
  • Deliveries: book, list, get one, cancel, and (test keys only) move a pretend delivery on.
  • Guest bookings for shoppers who pay CEND directly: start one and read its status.
  • verifyWebhook checks a webhook call and returns the event, or fails if it is not from CEND or is too old.
  • A failed call raises an error with the HTTP status, the field errors on a 422, and retryAfter on a 429.

Keep your key safe

  • Use the secret key on your server only, from an environment variable. Never put it in a web page or a mobile app.
  • For a web page, use the website widgets with a publishable key. It can price but cannot book.
  • A booking needs an idempotency key of 8 to 80 characters. The libraries make one for you if you do not pass your own; pass your order number so a retry of the same order is always safe.

Stuck, or want something changed?

Tell us what you are building and we will reply within one working day.