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-ghtk

v0.2.0

Published

TypeScript SDK for Giao Hang Tiet Kiem OpenAPI

Readme

@redonvn/shipping-ghtk

TypeScript SDK cho GHTK OpenAPI, hỗ trợ Node.js 18 trở lên.

Cài đặt

npm install @redonvn/shipping-ghtk
import { GhtkClient } from '@redonvn/shipping-ghtk';

const ghtk = new GhtkClient({
  token: process.env.GHTK_TOKEN,
  partnerCode: process.env.GHTK_PARTNER_CODE,
  referToken: process.env.GHTK_REFER_TOKEN, // nếu dùng luồng B2C
  environment: 'staging',
});

const fee = await ghtk.fees.calculate({
  pick_province: 'Hà Nội', pick_ward: 'Phường Mai Động',
  province: 'TP. Hồ Chí Minh', ward: 'Phường Bến Nghé',
  address: '123 Lê Lợi', weight: 1000, value: 300000,
});

Biến môi trường

Sao chép packages/shipping-ghtk/.env.example sang .env và điền giá trị thực. Không commit .env lên git.

| Biến | Mô tả | Bắt buộc | |------|-------|----------| | GHTK_TOKEN | API token lấy từ trang khách hàng GHTK (màn hình cấu hình API) | Có | | GHTK_PARTNER_CODE | Mã đối tác / shop code, gửi qua header X-Client-Source | Có | | GHTK_REFER_TOKEN | Token nền tảng B2C, gửi qua X-Refer-Token | Khi dùng luồng B2C | | GHTK_ENVIRONMENT | Môi trường: staging hoặc production (mặc định staging) | Không | | GHTK_BASE_URL | Base URL tuỳ chọn, override host mặc định của môi trường | Không | | GHTK_TIMEOUT_MS | Timeout cho mỗi request, đơn vị ms (mặc định 15000) | Không | | GHTK_MAX_RETRIES | Số lần retry tối đa cho request GET (mặc định 2) | Không | | GHTK_WEBHOOK_TOKEN | Token dùng xác thực webhook GHTK POST đến ứng dụng của bạn | Không |

Resources

  • auth.check() kiểm tra token.
  • shops.create() tạo shop B2C; shops.getToken() lấy token shop đã đăng ký.
  • fees.calculate() tính phí, trọng lượng dùng gram.
  • orders.create(), getStatus(), cancel() và cancelByPartnerId().
  • labels.downloadPdf() tải PDF A5/A6; downloadMultiplePdf() tải tối đa 100 nhãn.
  • catalog lấy kho, địa chỉ cấp 4, giải pháp và tra thông tin sản phẩm.
  • webhook.parse() parse JSON hoặc form URL encoded.

API đăng đơn có thể dùng weight_option: 'gram'; nếu không truyền, GHTK mặc định trọng lượng sản phẩm là kg. getPrintUrl() tồn tại để tương thích IShippingProvider, nhưng URL vẫn cần header Token; nên dùng labels.downloadPdf().

SDK không tự tính chữ ký webhook. Khi cấu hình webhookToken, ứng dụng cần chuyển hash/token nhận từ callback URL vào header x-ghtk-webhook-token khi gọi parseAndVerifyWebhook().

Tài liệu

Tài liệu API nguồn: GHTK OpenAPI và GHTK Pro OpenAPI.

Type và thay đổi hành vi

Request/response có type công khai cho từng API. GhtkOrderPayload yêu cầu street hoặc hamlet; use_return_address: 1 yêu cầu địa chỉ trả đầy đủ. Tính phí chấp nhận pick_address_id thay cho thông tin tỉnh/phường của điểm lấy; địa chỉ cấp 4 chấp nhận address đầy đủ.

Các mã 7, 8, 10, 13 lần lượt trả PICKUP_FAILED, PICKUP_DELAYED, DELIVERY_DELAYED, COMPENSATED. Thông báo shipper trả kind: 'shipper_notification', isAuthoritative: false, status: SHIPPER_NOTIFICATION; mã chưa biết trả UNKNOWN. Chỉ dùng sự kiện isAuthoritative: true để ghi đè trạng thái chính thức. Đây là thay đổi hành vi so với mapper cũ.

Các ID JSON nguyên lớn ở solution_id, tracking_id, product_code được giữ thành chuỗi nếu vượt giới hạn số nguyên an toàn JavaScript. Khi truyền ID lớn, luôn dùng chuỗi; SDK từ chối số đã vượt giới hạn an toàn.

getPrintUrl() từ chối nhiều mã để tránh bỏ qua các mã phía sau; dùng labels.downloadMultiplePdf() cho mã rút gọn. Endpoint in nhiều nhãn là phần mở rộng đã có trong SDK, không được mô tả trong bộ Express docs dùng để đối chiếu.

SDK không tự đọc .env; ứng dụng truyền các biến vào constructor. Xem bảng đối chiếu docs/API và giới hạn nguồn.

Kiểm tra

npm test --workspace=@redonvn/shipping-ghtk

Lệnh chạy build, test hồi quy, test contract bằng mock HTTP và kiểm tra type với các trường hợp hợp lệ/không hợp lệ. Không tạo shop hoặc đơn hàng thật.