-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-postcard.js
More file actions
58 lines (51 loc) · 1.75 KB
/
send-postcard.js
File metadata and controls
58 lines (51 loc) · 1.75 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
// Send a single postcard via the Postcard.bot API
// Usage: POSTCARDBOT_API_KEY=pk_live_your_key node send-postcard.js
const API_KEY = process.env.POSTCARDBOT_API_KEY;
const BASE_URL = process.env.POSTCARDBOT_API_URL || "https://postcard.bot";
if (!API_KEY) {
console.error("Set POSTCARDBOT_API_KEY environment variable");
console.error("Get your key at https://postcard.bot/account");
process.exit(1);
}
async function sendPostcard() {
const response = await fetch(`${BASE_URL}/api/v1/postcards`, {
method: "POST",
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
to: {
name: "Jane Doe",
address_line1: "123 Main St",
city: "San Francisco",
state: "CA",
zip: "94102",
country: "US",
},
from: {
name: "John Smith",
address_line1: "456 Oak Ave",
city: "New York",
state: "NY",
zip: "10001",
},
message: "Wish you were here! Having an amazing time.",
image_url: "https://images.unsplash.com/photo-1501594907352-04cda38ebc29?w=1875",
}),
});
const data = await response.json();
if (!response.ok) {
console.error("Error:", data.error);
if (data.details) console.error("Details:", data.details);
if (data.top_up_url) console.error("Add balance:", data.top_up_url);
process.exit(1);
}
console.log("Postcard sent!");
console.log(` ID: ${data.id}`);
console.log(` Service: ${data.service}`);
console.log(` Price: $${data.price.toFixed(2)}`);
console.log(` Balance remaining: $${data.balance_remaining.toFixed(2)}`);
console.log(` Expected delivery: ${data.expected_delivery_date || "TBD"}`);
}
sendPostcard().catch(console.error);