Next.js Integration
The Bigship SDK is a server-side package. In Next.js, use it in Route Handlers or Server Actions — never in "use client" components.
Add the SDK to transpilePackages in your next.config.js:
/** @type {import('next').NextConfig} */const nextConfig = { transpilePackages: ['@agamya/bigship-sdk'],};
module.exports = nextConfig;Route Handler — Create Order
Section titled “Route Handler — Create Order”import { NextRequest, NextResponse } from 'next/server';import { BigshipClient, isFailedResponse } from '@agamya/bigship-sdk';
const client = new BigshipClient({ baseURL: process.env.BIGSHIP_BASE_URL!, userName: process.env.BIGSHIP_USERNAME!, password: process.env.BIGSHIP_PASSWORD!, accessKey: process.env.BIGSHIP_ACCESS_KEY!,});
export async function POST(request: NextRequest) { const body = await request.json();
const order = await client.createOrder({ segment_type: 'domestic_b2c', MasterOrderPickUpLocation: body.pickup_location_id, MasterOrderReturnLocation: body.pickup_location_id, MasterOrderDate: new Date().toISOString().replace('T', ' ').substring(0, 19), MasterOrderPaymentMode: body.payment_type === 'COD' ? 2 : 1, OrderInvoiceNo: `INV-${Date.now()}`, MasterOrderInvoiceAmount: body.shipment_invoice_amount, MasterOrderShippingName: body.consignee_detail.name, MasterOrderShippingMobileNo: body.consignee_detail.phone, MasterOrderShippingEmail: body.consignee_detail.email, MasterOrderShippingAddress: body.consignee_detail.address, MasterOrderShippingZipCode: body.consignee_detail.pincode, MasterOrderShippingCity: body.consignee_detail.city, MasterOrderShippingState: body.consignee_detail.state, MasterOrderShippingCountry: 'India', totalNumOfBoxes: body.box_details?.length || 1, boxes: body.box_details || [], invoice_document_file: body.document_detail?.invoice_document_file, });
if (isFailedResponse(order)) { return NextResponse.json({ error: order.message }, { status: 400 }); }
return NextResponse.json({ orderId: order.data });}Route Handler — Track Shipment
Section titled “Route Handler — Track Shipment”import { NextRequest, NextResponse } from 'next/server';import { BigshipClient, isFailedResponse } from '@agamya/bigship-sdk';
const client = new BigshipClient({ baseURL: process.env.BIGSHIP_BASE_URL!, userName: process.env.BIGSHIP_USERNAME!, password: process.env.BIGSHIP_PASSWORD!, accessKey: process.env.BIGSHIP_ACCESS_KEY!,});
export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; const tracking = await client.trackOrder(id);
if (isFailedResponse(tracking)) { return NextResponse.json({ error: tracking.message }, { status: 404 }); }
return NextResponse.json({ status: tracking.data });}Server Action — Track Shipment
Section titled “Server Action — Track Shipment”'use server';
import { BigshipClient, isFailedResponse } from '@agamya/bigship-sdk';
const client = new BigshipClient({ baseURL: process.env.BIGSHIP_BASE_URL!, userName: process.env.BIGSHIP_USERNAME!, password: process.env.BIGSHIP_PASSWORD!, accessKey: process.env.BIGSHIP_ACCESS_KEY!,});
export async function trackByAwb(orderId: string) { const tracking = await client.trackOrder(orderId);
if (isFailedResponse(tracking)) { return { error: tracking.message }; }
return { data: tracking.data };}Use the Server Action in a client component:
'use client';
import { useState } from 'react';import { trackByAwb } from '@/app/actions/tracking';
export function TrackingForm() { const [orderId, setOrderId] = useState(''); const [result, setResult] = useState<string | null>(null);
async function handleSubmit(e: React.FormEvent) { e.preventDefault(); const response = await trackByAwb(orderId); if (response.error) { setResult(`Error: ${response.error}`); } else { setResult(JSON.stringify(response.data, null, 2)); } }
return ( <form onSubmit={handleSubmit}> <input value={orderId} onChange={(e) => setOrderId(e.target.value)} placeholder="Enter Order ID" /> <button type="submit">Track</button> {result && <pre>{result}</pre>} </form> );}Important
Section titled “Important”- Never use the SDK in
"use client"components — it requires server-side Node.js APIs - Always initialize
BigshipClientoutside request handlers to reuse the instance and benefit from token caching - Use environment variables for credentials — never hardcode them