交互式示例
通过多语言的分步代码示例,边做边学
入门
5 分钟
创建你的第一个运单
学习如何创建运单,包括地址验证和承运商选择
进阶
10 分钟
实时包裹追踪
通过 Webhook 实现追踪更新,自动推送通知
高级
15 分钟
批量生成面单
借助批量操作,高效处理数百个订单
创建你的第一个运单
学习如何创建运单,包括地址验证和承运商选择
入门
5 分钟
npm install @atoship/sdkimport { atoshipSDK } from '@atoship/sdk';
const atoship = new atoshipSDK({
apiKey: 'your_api_key'
});
async function createShipment() {
try {
// 创建运单
const shipment = await atoship.shipments.create({
from_address: {
name: "John Doe",
company: "atoship Inc",
street1: "123 Main St",
city: "San Francisco",
state: "CA",
zip: "94105",
country: "US",
phone: "415-123-4567"
},
to_address: {
name: "Jane Smith",
street1: "456 Oak Ave",
city: "Los Angeles",
state: "CA",
zip: "90001",
country: "US",
phone: "213-987-6543"
},
parcel: {
length: 10,
width: 8,
height: 4,
weight: 16 // 盎司
}
});
console.log('Shipment created:', shipment.id);
console.log('Tracking number:', shipment.tracking_number);
// 获取该运单的运费报价
const rates = await atoship.shipments.getRates(shipment.id);
console.log('Available rates:', rates);
// 选择最便宜的报价
const cheapestRate = rates.sort((a, b) => a.amount - b.amount)[0];
console.log('Selected rate:', cheapestRate);
// 购买面单
const label = await atoship.labels.create({
shipment_id: shipment.id,
rate_id: cheapestRate.id
});
console.log('Label URL:', label.label_url);
return label;
} catch (error) {
console.error('Error creating shipment:', error);
}
}
// 运行示例
createShipment();实用技巧:
- 创建运单前,务必先验证收发件地址
- 比较多家承运商的报价,拿到最优价格
- 保存好运单号,方便向客户发送通知