JavaScript 範例
示範如何在 Node.js 或瀏覽器環境透過 JavaScript 使用 Boring API。
安裝套件
npm install axios
# 或
npm install node-fetch
基礎設定(Node.js)
Axios
const axios = require('axios');
const API_URL = "https://boring.aiagent-me.com/v2/posts";
const API_KEY = process.env.BORING_API_KEY;
const headers = {
"boring-api-key": API_KEY,
"Content-Type": "application/json"
};
Fetch(Node.js 18+)
const API_URL = "https://boring.aiagent-me.com/v2/posts";
const API_KEY = process.env.BORING_API_KEY;
const headers = {
"boring-api-key": API_KEY,
"Content-Type": "application/json"
};
共用發佈函式
Axios 版本
async function publishPost(accountId, platform, text, mediaUrls = []) {
const data = {
post: {
accountId,
content: {
text,
mediaUrls,
platform
},
target: {
targetType: platform
}
}
};
const response = await axios.post(API_URL, data, { headers });
return response.data;
}
Fetch 版本
async function publishPost(accountId, platform, text, mediaUrls = []) {
const data = {
post: {
accountId,
content: {
text,
mediaUrls,
platform
},
target: {
targetType: platform
}
}
};
const response = await fetch(API_URL, {
method: "POST",
headers,
body: JSON.stringify(data)
});
const result = await response.json();
if (!result.success) {
throw new Error(result.message);
}
return result;
}
Facebook 範例
await publishPost("facebook-account-id", "facebook", "Hello from Boring API! 🚀");
await publishPost("facebook-account-id", "facebook", "Check this out!", ["https://example.com/photo.jpg"]);
await publishPost("facebook-account-id", "facebook", "Album recap!", [
"https://example.com/photo1.jpg",
"https://example.com/photo2.jpg"
]);
await publishPost("facebook-account-id", "facebook", "Watch the demo!", ["https://example.com/demo.mp4"]);
Instagram 範例
await publishPost("instagram-account-id", "instagram", "Daily highlight 🌟", ["https://example.com/photo.jpg"]);
await publishPost("instagram-account-id", "instagram", "Swipe for more ➡️", [
"https://example.com/1.jpg",
"https://example.com/2.jpg"
]);
await publishPost("instagram-account-id", "instagram", "New reel!", ["https://example.com/reel.mp4"]);
Threads 範例
await publishPost("threads-account-id", "threads", "Quick update for everyone!");
await publishPost("threads-account-id", "threads", "Behind the scenes 🎬", ["https://example.com/photo.jpg"]);
await publishPost("threads-account-id", "threads", [
"🧵 Part 1: Why automation matters",
"Part 2: How we built it",
"Part 3: What's next?"
]);
X(Twitter)範例
await publishPost("x-account-id", "x", "Launch day! 🚀 #startup");
await publishPost("x-account-id", "x", "Photo drop!", ["https://example.com/photo1.jpg"]);
await publishPost("x-account-id", "x", "Video teaser", ["https://example.com/video.mp4"]);
YouTube 範例
await publishPost(
"youtube-account-id",
"youtube",
"Product Walkthrough 2025\n\nFull demo with #hashtag",
["https://example.com/video.mp4", "https://example.com/thumbnail.jpg"]
);
錯誤處理
try {
const result = await publishPost(...);
console.log(result);
} catch (error) {
console.error("Publish failed:", error.response?.data || error.message);
}
重試機制
async function publishWithRetry(params, retries = 3) {
for (let attempt = 0; attempt < retries; attempt++) {
try {
return await publishPost(...params);
} catch (error) {
const code = error.response?.data?.error;
if (!["RateLimitExceeded", "ServiceUnavailable", "Timeout"].includes(code)) {
throw error;
}
const wait = Math.pow(2, attempt);
await new Promise(r => setTimeout(r, wait * 1000));
}
}
throw new Error("MaxRetriesExceeded");
}
批次發佈
const posts = [
{ accountId: "fb", platform: "facebook", text: "Hello FB!" },
{ accountId: "ig", platform: "instagram", text: "Hello IG!", mediaUrls: ["https://..."] }
];
for (const post of posts) {
await publishPost(post.accountId, post.platform, post.text, post.mediaUrls);
}
瀏覽器 Demo(僅限測試)
<!-- 請勿在正式環境暴露 API Key -->
<form id="publishForm">...</form>
<script>
// 省略:同原始範例
</script>
生產環境請改由後端代理請求,避免洩漏 API Key。
完整腳本(Node.js)
#!/usr/bin/env node
const axios = require('axios');
const API_URL = "https://boring.aiagent-me.com/v2/posts";
const API_KEY = process.env.BORING_API_KEY;
const headers = {
"boring-api-key": API_KEY,
"Content-Type": "application/json"
};
async function publishPost(accountId, platform, text, mediaUrls = []) {
const data = {
post: {
accountId,
content: { text, mediaUrls, platform },
target: { targetType: platform }
}
};
const response = await axios.post(API_URL, data, { headers });
return response.data;
}
async function main() {
console.log("Publishing to Facebook...");
const result = await publishPost(process.env.FB_ACCOUNT_ID, "facebook", "Hello from Boring API! 🚀");
console.log(result);
}
main().catch(console.error);