JavaScript / Node.js
Use fetch nativo (Node 18+, browser, edge runtimes). Sem dependências externas.
Rastreamento
Detecção automática Correios vs Total Express pelo formato do código.
async function rastrear(codigo) {
const res = await fetch(
`https://seurastreio.com.br/api/public/rastreio/${codigo}`,
{ headers: { Authorization: "Bearer sr_live_sua_chave_aqui" } },
);
if (!res.ok) {
const err = await res.json();
throw new Error(`${err.code}: ${err.message}`);
}
return res.json();
}
const dados = await rastrear("BR123456789BR");
console.log(dados.eventoMaisRecente.descricao);
// Plano pago: dados.historico, dados.previsaoEntrega
Consulta de CEP
async function buscarCep(cep) {
const limpo = cep.replace(/\D/g, "");
const res = await fetch(
`https://seurastreio.com.br/api/public/cep/${limpo}`,
{ headers: { Authorization: "Bearer sr_live_sua_chave_aqui" } },
);
const json = await res.json();
if (!json.success) return null;
return json.data; // { logradouro, bairro, localidade, uf, ... }
}
console.log(await buscarCep("01310-100"));Criar Logística Reversa
async function criarLR(payload) {
const res = await fetch("https://seurastreio.com.br/api/public/logistica-reversa", {
method: "POST",
headers: {
Authorization: "Bearer sr_live_sua_chave_aqui",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const json = await res.json();
if (!json.ok) throw new Error(`${json.code}: ${json.message}`);
return json; // { id, idCliente, numeroEtiqueta, prazoValidade, ... }
}
await criarLR({
credentialId: "9a2c1f80-1234-4abc-9def-000000000001",
tipo: "A",
servicoEnvio: "pac",
remetente: {
nome: "Maria Silva",
logradouro: "Rua das Flores",
numero: "100",
complemento: "",
bairro: "Centro",
cidade: "São Paulo",
uf: "SP",
cep: "01310100",
ddd: "11",
telefone: "987654321",
email: "[email protected]",
},
});Listar com paginação
O endpoint usa cursor — repita até nextCursor ser null.
async function listarTodasLRs() {
const todas = [];
let cursor;
do {
const url = new URL("https://seurastreio.com.br/api/public/logistica-reversa");
url.searchParams.set("limit", "100");
if (cursor) url.searchParams.set("cursor", cursor);
const res = await fetch(url, {
headers: { Authorization: "Bearer sr_live_sua_chave_aqui" },
});
const { items, nextCursor } = await res.json();
todas.push(...items);
cursor = nextCursor;
} while (cursor);
return todas;
}Tratamento de erro robusto
async function call(url, init) {
const res = await fetch(url, init);
const json = await res.json().catch(() => ({}));
if (res.status === 401) throw new Error("Chave de API inválida");
if (res.status === 429) {
const { current, max } = json;
throw new Error(`Cota mensal estourada (${current}/${max})`);
}
if (!res.ok) throw new Error(json.message ?? `HTTP ${res.status}`);
return json;
}Em ambientes serverless (Vercel, Cloudflare Workers, Edge), o
fetch nativo já basta — não inclua node-fetch ou axios só para isso.