-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
117 lines (108 loc) · 3.5 KB
/
example.js
File metadata and controls
117 lines (108 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
This example demonstrates how to purchase a label for an international shipment.
Creating domestic shipment would follow a similiar proccess but would not require
the creation of CustomsItems and CustomsDeclaration objects.
**/
// replace <YOUR_PRIVATE_KEY> with your ShippoToken key
var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');
var addressFrom = {
"name":"Ms Hippo",
"company":"Shippo",
"street1":"215 Clayton St.",
"city":"San Francisco",
"state":"CA",
"zip":"94117",
"country":"US", //iso2 country code
"phone":"+1 555 341 9393",
"email":"ms-hippo@goshippo.com",
}
// example address_to object dict
var addressTo = {
"name":"Mr Hippo",
"company":"London Zoo",
"street1":"Regent's Park",
"street2":"Outer Cir",
"city":"LONDON",
"state":"",
"zip":"NW1 4RY",
"country":"GB", //iso2 country code
"phone":"+1 555 341 9393",
"email":"mrhippo@goshippo.com",
"metadata" : "Hippo T-Shirt Order #1043"
}
// parcel object dict
var parcel = {
"length":"5",
"width":"5",
"height":"5",
"distance_unit":"in",
"weight":"2",
"mass_unit":"lb",
}
// example CustomsItems object. This is required for int'l shipment only.
var customsItem = {
"description":"T-Shirt",
"quantity":2,
"net_weight":"0.3",
"mass_unit":"lb",
"value_amount":"20",
"value_currency":"USD",
"origin_country":"US",
}
// Creating the CustomsDeclaration
// (CustomsDeclaration are NOT required for domestic shipments)
shippo.customsdeclaration.create({
"contents_type": "MERCHANDISE",
"non_delivery_option": "RETURN",
"certify": true,
"certify_signer": "Mr. Hippo",
"items": [customsItem],
})
.then(function(customsDeclaration) {
console.log("customs Declaration : %s", JSON.stringify(customsDeclaration, null, 4));
// Creating the shipment object. In this example, the objects are directly passed to the
// shipment.create method, Alternatively, the Address and Parcel objects could be created
// using address.create(..) and parcel.create(..) functions respectively.
// adding the async:false makes this call synchronous
return shippo.shipment.create({
"address_from": addressFrom,
"address_to": addressTo,
"parcels": [parcel],
"customs_declaration": customsDeclaration.object_id,
"async": false
})
}, function(err) {
// Deal with an error
console.log("There was an error creating customs declaration: %s", err);
})
.then(function(shipment) {
console.log("shipment : %s", JSON.stringify(shipment, null, 4));
shippo.shipment.rates(shipment.object_id)
.then(function(rates) {
console.log("rates : %s", JSON.stringify(rates, null, 4));
// Get the first rate in the rates results for demo purposes.
rate = rates.results[0];
// Purchase the desired rate
return shippo.transaction.create({"rate": rate.object_id, "async": false})
}).catch(function(err) {
// Deal with an error
console.log("There was an error retrieving rates : %s", err);
})
.then(function(transaction) {
console.log("transaction : %s", JSON.stringify(transaction, null, 4));
// print label_url and tracking_number
if(transaction.status == "SUCCESS") {
console.log("Label URL: %s", transaction.label_url);
console.log("Tracking Number: %s", transaction.tracking_number);
}else{
//Deal with an error with the transaction
console.log("Message: %s", JSON.stringify(transaction.messages, null, 2));
}
}).catch(function(err) {
// Deal with an error
console.log("There was an error creating transaction : %s", err);
});
}).catch(function(err) {
// Deal with an error
console.log("There was an error creating shipment: %s", err);
});