Ejemplos interactivos
Aprende haciendo, con ejemplos de código paso a paso en varios lenguajes
Principiante
5 min
Compra tu primera etiqueta
Cotizar, crear el borrador, comprar: las tres llamadas que producen una etiqueta real
Intermedio
10 min
Sigue un envío
Lee el estado de un envío y registra un webhook para no gastar peticiones haciendo polling
Avanzado
15 min
Compra etiquetas por lotes
Hasta 25 etiquetas en una petición, y un solo cargo de límite en vez de 75
Compra tu primera etiqueta
Cotizar, crear el borrador, comprar: las tres llamadas que producen una etiqueta real
Principiante
5 min
node >= 18 — fetch is built in, no SDK neededconst API = 'https://atoship.com/api/v1';
const KEY = process.env.ATOSHIP_API_KEY;
async function call(path, body) {
const res = await fetch(API + path, {
method: 'POST',
headers: { Authorization: `Bearer ${KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify(body ?? {})
});
const data = await res.json();
// Every error carries error.code (UPPER_SNAKE_CASE). Branch on that —
// never on the message text, which is free to change.
if (!res.ok) throw new Error(`${data.error?.code}: ${data.error?.message}`);
return data;
}
const shipment = {
from_address: {
name: 'Warehouse', street1: '417 Montgomery St',
city: 'San Francisco', state: 'CA', zip: '94104', country: 'US'
},
to_address: {
name: 'Ada Lovelace', street1: '1355 Market St',
city: 'San Francisco', state: 'CA', zip: '94103', country: 'US'
},
parcel: {
length: 10, width: 8, height: 6,
weight: 32, weight_unit: 'oz', dimension_unit: 'in'
}
};
async function buyLabel() {
// 1. Quote. Rating is free — it spends nothing and commits to nothing.
const { data: rates } = await call('/rates', shipment);
if (!rates.length) throw new Error('No rates for this shipment');
const cheapest = rates.reduce((a, b) => (a.rate <= b.rate ? a : b));
console.log(`${cheapest.carrier} ${cheapest.service} $${cheapest.rate}`);
// 2. Create the label as a DRAFT. Still nothing charged.
const draft = await call('/labels', { ...shipment, rate_id: cheapest.id });
console.log(draft.id, draft.status); // 'draft', label_url still null
// 3. Purchase. THIS is the call that spends money.
const label = await call(`/labels/${draft.id}/purchase`);
console.log(label.status, label.tracking_number, label.label_url);
return label;
}
buyLabel().catch((err) => console.error(err.message));Consejos profesionales:
- Cotizar es gratis. Pide tarifas las veces que quieras: solo la llamada de compra gasta dinero.
- Reutiliza el rate_id de la cotización. Sin él, el servidor elige el servicio, y rara vez es el que cotizaste.
- La etiqueta se crea primero como borrador, así una dirección mala falla antes de que se cobre nada.
Próximos pasos
¿Listo para implementar estos ejemplos en tu aplicación?