Bỏ qua, tới nội dung chính

Xác thực

Gửi khóa trong header api-key.

SDK chưa phát hành. Nội dung dưới đây dùng HTTP thuần.

Nhóm tối ưu tuyến nhận khóa API qua header api-key, không nhận Bearer token. Khóa phải còn hiệu lực và được bật quyền, và tính năng phải đang mở ở phía máy chủ.

const BASE = "https://<your-host>/api/v1/partners";

// Route optimisation authenticates with the api-key header, lowercase.
// It is not a Bearer token. The other groups take Authorization: Bearer
// with a JWT, and tour planning accepts either.
const headers = { "api-key": process.env.ROUTE4GREEN_API_KEY! };

// The cheapest request that proves a key works end to end: list your own
// jobs. Three things must be true at once for it to answer: the key is
// valid and not expired, the key has been granted Route optimization
// access by the Route4Green team, and the feature is switched on server
// side. If any one is false the request is refused with a code from the
// error tables in the reference.
const response = await fetch(`${BASE}/addons/route-optimization/jobs`, { headers });

// Branch on the envelope's success flag first, never on response.ok
// alone: some failures arrive as HTTP 200 with success false.
const envelope = await response.json();
if (!envelope.success) {
  // error_code is a string on some responses and a number on others.
  throw new Error(`${String(envelope.error_code)}: ${envelope.message}`);
}

console.log("key is live; jobs visible:", envelope.data);

Phần giải thích đầy đủ, gồm cả cách lấy khóa, nằm ở mục xác thực trên trang tài liệu chính.

Đọc thêm