npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@redonvn/shipping-goship

v0.2.0

Published

TypeScript SDK for Goship Open API v2

Readme

Goship Open API v2 SDK

TypeScript SDK cho API Goship, chạy trên Node.js 18 trở lên. Mặc định dùng sandbox; đặt environment: 'production' để dùng môi trường thật. SDK trả về JSON của Goship cùng kiểu TypeScript, không tự sửa nội dung phản hồi.

Gói giữ API gốc của Goship. Các SDK shipping-* khác có adapter IShippingProvider, nhưng Goship chưa công bố API in nhãn và DTO chung chưa có đủ mã địa bàn/kích thước bắt buộc cho luồng tạo đơn. Vì vậy gói này chưa khai báo adapter đó; tracking_url là link theo dõi, không phải nhãn in.

Đăng ký tài khoản và lấy thông tin API

  1. Truy cập Goship Sandbox, chọn Đăng ký, điền thông tin trong biểu mẫu và gửi để tạo tài khoản. Đây là tài khoản quản lý vận đơn dùng khi thử nghiệm tích hợp API.
  2. Đăng nhập vào hệ thống, mở Cài đặt → Kết nối API. Tại đây có hai cách lấy thông tin xác thực:
    • Access Token trực tiếp: sao chép token và đặt vào biến môi trường GOSHIP_ACCESS_TOKEN. Cách này đủ cho trường hợp máy chủ của bạn gọi Goship API bằng một tài khoản.
    • Tài khoản developer: trong mục Goship API Account, bấm Generate để tạo Client ID và Client Secret. Lưu chúng vào GOSHIP_CLIENT_ID và GOSHIP_CLIENT_SECRET; dùng thêm tên đăng nhập và mật khẩu Goship (GOSHIP_USERNAME, GOSHIP_PASSWORD) để gọi auth.login() lấy access token.
  3. Khi chuyển sang môi trường thật, dùng Goship Production và yêu cầu Goship kích hoạt tài khoản developer production theo hướng dẫn thiết lập chính thức. Thông tin Sandbox và Production là hai môi trường riêng.

Giữ token, mật khẩu và Client Secret trong biến môi trường hoặc kho bí mật của máy chủ. Xem thêm hướng dẫn xác thực của Goship.

Cài đặt và cấu hình

npm install @redai/shipping-goship
import { GoshipClient, GoshipWebhook, mapGoshipStatus } from '@redai/shipping-goship';

const goship = new GoshipClient({ environment: 'sandbox', accessToken: process.env.GOSHIP_ACCESS_TOKEN, webhookSecret: process.env.GOSHIP_CLIENT_SECRET });
const cities = await goship.locations.cities();

Nếu chọn tài khoản developer thay vì lấy Access Token trực tiếp, bỏ accessToken trong constructor và đăng nhập trước khi gọi API; SDK lưu token nhận được cho các lệnh tiếp theo:

const goship = new GoshipClient({ environment: 'sandbox', webhookSecret: process.env.GOSHIP_CLIENT_SECRET });
await goship.auth.login({
  username: process.env.GOSHIP_USERNAME!,
  password: process.env.GOSHIP_PASSWORD!,
  client_id: process.env.GOSHIP_CLIENT_ID!,
  client_secret: process.env.GOSHIP_CLIENT_SECRET!,
});
const cities = await goship.locations.cities();

Base URL: sandbox https://sandbox.goship.io/api/v2, production https://api.goship.io/api/v2. Có thể đặt baseUrl, timeoutMs, maxRetries, fetch trong constructor. Chỉ GET được thử lại khi gặp lỗi mạng, timeout, 408, 429 hoặc 5xx; các lệnh ghi không tự thử lại để tránh tạo trùng đơn.

Toàn bộ endpoint được tài liệu hóa

| Nhóm | Hàm SDK | HTTP endpoint | | --- | --- | --- | | Xác thực | auth.login | POST /login | | Địa điểm | locations.cities | GET /cities | | Địa điểm | locations.districts | GET /districts | | Địa điểm | locations.cityDistricts | GET /cities/{code}/districts | | Địa điểm | locations.districtWards | GET /districts/{code}/wards | | Khách hàng | customers.list, customers.search | GET /customers, GET /customers/search | | Khách hàng | customers.create, customers.update, customers.delete | POST /customers, POST /customers/{id}, DELETE /customers/{id} | | Báo giá | rates.calculate | POST /rates | | Vận đơn thường | shipments.list, shipments.search | GET /shipments, GET /shipments/search | | Vận đơn thường | shipments.create, shipments.cancel | POST /shipments, DELETE /shipments/{id} | | Hóa đơn | invoices.list, invoices.search, invoices.shipments | GET /invoices, GET /invoices/search, GET /invoices/{code}/shipments | | Giao dịch | transactions.list | GET /transactions | | Đăng ký webhook | webhookSubscriptions.list, .create, .delete | GET /webhooks, POST /webhooks, DELETE /webhooks/{id} | | OnDemand báo giá | onDemandRates.calculate | POST /ondemand-shipments/rates | | OnDemand vận đơn | onDemandShipments.list, .get, .create, .update, .cancel | GET /ondemand-shipments, GET /ondemand-shipments/{id}, POST /ondemand-shipments, PUT /ondemand-shipments/{id}, DELETE /ondemand-shipments/{id} |

Các trang carrier và status code là bảng mã tham chiếu, không phải endpoint. Tài liệu hiện không nêu endpoint in vận đơn, xem chi tiết vận đơn thường, cập nhật vận đơn thường, hoặc đăng ký webhook riêng cho OnDemand.

Luồng vận đơn thường

const quote = await goship.rates.calculate({
  shipment: {
    address_from: { city: '79', district: '760' },
    address_to: { city: '01', district: '001' },
    parcel: { cod: 0, amount: 150000, weight: 500, width: 10, height: 10, length: 10 },
  },
});
const rateId = quote.data[0].id;
const accepted = await goship.shipments.create({
  shipment: {
    rate: rateId,
    payer: 0,
    order_id: 'ORDER-123',
    address_from: { city: '79', district: '760', name: 'Người gửi', phone: '0900000000', street: '1 Nguyễn Huệ' },
    address_to: { city: '01', district: '001', name: 'Người nhận', phone: '0911111111', street: '2 Tràng Tiền' },
    parcel: { cod: 0, amount: 150000, weight: 500, width: 10, height: 10, length: 10, name: 'Hàng mẫu' },
  },
});

weight tính bằng gram, kích thước width/height/length tính bằng cm. rate là ID trả về từ /rates. Tạo đơn thường là bất đồng bộ: HTTP 200 chưa bảo đảm hãng vận chuyển đã nhận. Lưu id của Goship, theo dõi webhook và kiểm tra shipment_status === 900 cùng carrier_error khi hãng từ chối. tracking_number là mã của hãng, có thể xuất hiện sau. order_id là mã nội bộ của cửa hàng. Khi hủy, truyền id Goship.

Webhook

const webhook = new GoshipWebhook(process.env.GOSHIP_CLIENT_SECRET!);
// rawBody phải là nguyên văn bytes nhận từ HTTP, trước mọi JSON parser.
const payload = webhook.verifyAndParse(rawBody, request.headers as Record<string, string | undefined>);
const normalized = mapGoshipStatus(payload.status); // true ở đối số thứ hai cho OnDemand

Chữ ký ở header x-goship-hmac-sha256, được kiểm tra dạng Base64(HMAC-SHA256(raw body, client secret)) theo ví dụ PHP trong tài liệu. Hãy đối chiếu bằng một webhook thật trước khi chạy production vì ví dụ Node.js trong tài liệu không nhất quán với ví dụ PHP. Sandbox không gửi webhook. Xử lý sự kiện idempotent theo mã đơn và trạng thái/thời điểm cập nhật. Mã trạng thái thường và OnDemand khác nhau; mapGoshipStatus trả UNKNOWN cho trạng thái đối soát hoặc trạng thái mơ hồ.

Phản hồi và lỗi

Các hàm giữ nguyên envelope code, status, data, meta.pagination khi tài liệu trả envelope. Một số ví dụ tạo khách hàng/vận đơn không thống nhất envelope, nên kiểu trả về của chúng là union. Lỗi HTTP hoặc status: 'error'/code >= 400 ném GoshipApiError với statusCode, code, path, rawResponse. Hạn mức theo tài liệu là 300 request/phút; SDK chưa tự điều tiết tốc độ.

Nguồn tài liệu