@dropzero/sdk
v0.3.0
Published
JavaScript/TypeScript client for the Dropzero API. Zero dependencies; works in Node 18+, Bun, Deno, Cloudflare Workers and browsers.
Maintainers
Readme
@dropzero/sdk
JavaScript / TypeScript client for the DropZero API. Zero dependencies. Node 18+, Bun, Deno, Cloudflare Workers and browsers (use a test key in a browser; never ship a live key to one).
npm install @dropzero/sdkPartner platforms pass a dzp_ key and call dz.forMerchant(id) for merchant requests.
import { Dropzero } from '@dropzero/sdk'
const dz = new Dropzero({
apiKey: process.env.DROPZERO_API_KEY!, // dz_test_... for the sandbox
baseUrl: process.env.DROPZERO_API_URL!, // your API URL; there is no built-in default host
})
const { items } = await dz.products.list({ q: 'kettle' })
const order = await dz.orders.create({
customer: { name: 'Rafi Ahmed', phone: '01712345678', address: 'House 4, Road 2, Dhanmondi', district: 'Dhaka' },
items: [{ product_id: items[0].id, quantity: 1, unit_price_cents: 189000 }], // ৳1,890.00
shipping_cents: 6000,
})
console.log(order.number, order.total_cents, order.margin_cents)Money is always integer paisa (100 paisa = ৳1).
Test mode
A dz_test_ key uses the sandbox: orders are not real and use no stock. Move them through every status yourself:
for (const s of ['confirmed', 'packed', 'shipped', 'delivered'] as const) await dz.sandbox.advance(order.id, s)Webhooks
import { constructEvent } from '@dropzero/sdk'
// Express: use express.text({ type: 'application/json' }) so you keep the RAW body
app.post('/dropzero-webhook', async (req, res) => {
try {
const event = await constructEvent(req.body, req.header('Dropzero-Signature'), process.env.DROPZERO_WEBHOOK_SECRET!)
// event.id is unique: ignore ones you have already handled
if (event.type === 'order.shipped') console.log(event.data.order.tracking_number)
res.sendStatus(200)
} catch {
res.sendStatus(400)
}
})Events
| Events | event.data |
|---|---|
| order.created, .confirmed, .packed, .shipped, .delivered, .cancelled, .returned | order; partner endpoints also get merchant |
| return.requested, .approved, .rejected, .received | return, order, merchant (live mode only; a return may cover part of an order) |
| product.updated | product |
| product.unavailable | product: id, sku, name |
| ping | message |
An endpoint with no event list receives every event, including ones added later: check event.type and ignore what you
do not handle. Stock changes caused by orders and returns do not send product.updated. There is no refund event.
Upgrading from 0.2: orders.cancel(id, reason?) and sandbox.advance(id, status) now call PATCH
/api/v1/orders/{id} (status: "cancelled") and PATCH /api/v1/sandbox/orders/{id}; the old POST .../cancel and
.../advance routes are gone, so upgrade together with the API. Products now carry packaging_cents (Dropzero's
packaging fee per unit: your full cost is supplier_price_cents + packaging_cents) and, when a price rise is announced,
upcoming_price_cents and upcoming_price_at (also on variants). Orders carry packaging_cents, and margin_cents is
net of it.
Upgrading from 0.1: OrderEventPayload.type is now OrderEventType, and WebhookEvent includes return and product
events, so check event.type before reading event.data.order.
Behaviour worth knowing
- Errors: every non-2xx throws
DropzeroErrorwithstatus,code(stable, branch on this) andrequestId. A request that never got an answer throwsDropzeroConnectionError. - Retries: network errors,
429(honouringRetry-After) and5xxare retried twice by default, only for requests that are safe to repeat: reads, deletes, andorders.create(which always sends anIdempotency-Key, yours or a random one, and reuses it on retry, so a retry can never create a second order). - Pagination:
for await (const p of dz.products.iterate()) { ... }walks every page. - Options:
baseUrl(required, orDROPZERO_API_URL),timeoutMs(30s),maxRetries(2),fetch.
Full guides and the API reference are at https://docs.dropzero.bd (sign in with your DropZero account).
