> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Node.js NPM

> 使用 Social API NPM 套件將 Ayrshare API 整合到你的 Node 應用程式

## 概觀

Ayrshare 的 [Social API NPM 套件](https://www.npmjs.com/package/social-media-api) 讓你可以將 Ayrshare API 整合到 Node.js 應用程式中。

### 安裝

如果你在伺服器端使用 Node，請安裝 Social API NPM 套件。
該套件封裝了 RESTful 呼叫，簡化了呼叫方式。

```bash theme={"system"}
npm i social-media-api
```

<Info>請在 [Ayrshare 儀表板](https://app.ayrshare.com/) 取得你的密鑰 API Key。</Info>

### 一般使用方式

Post、History 與 Delete 的範例：

<Info>
  **要發布到 X/Twitter？** 自 2026 年 3 月 31 日起，透過 Ayrshare 進行 X/Twitter 操作都需要使用你自己的 X Developer App 憑證——Ayrshare 對每一個發往 X 的呼叫都會強制執行此規則。請在請求中加入 2 個 BYO 標頭。詳情請參閱 [設定指南](/dashboard/connect-social-accounts/x-twitter-byo-keys)。
</Info>

從 v1.3.0 起，SDK 內建 `setTwitterByo(apiKey, apiSecret)` 輔助函式，會為之後每個請求附加兩個必要的 `X-Twitter-OAuth1-*` 標頭：

```javascript theme={"system"}
const SocialPost = require("social-media-api");
const social = new SocialPost(API_KEY)
  .setTwitterByo(MY_X_API_KEY, MY_X_API_SECRET);

await social.post({
  post: "Hello from BYO",
  platforms: ["twitter"]
});
```

使用 `clearTwitterByo()` 移除這些標頭——當單一 SDK 實例要在不同租戶間重複使用時很有用：

```javascript theme={"system"}
social.clearTwitterByo().setTwitterByo(nextTenant.key, nextTenant.secret);
```

```javascript theme={"system"}
const SocialPost = require("social-media-api");
const API_KEY = "API KEY"; // get an API Key at ayrshare.com
const social = new SocialPost(API_KEY);

const run = async () => {
  /** post */
  const post = await social
    .post({
      post: "One more time",
      platforms: ["twitter", "facebook", "linkedin"],
      profileKey: "DJKJDK-SKDJKDJF" // used with a User Profile
    })
    .catch(console.error);
  console.log(post);

  /** history */
  const history = await social.history().catch(console.error);
  console.log(history);

  /** delete */
  const deletePost = await social.delete({ id: post.id }).catch(console.error);
  console.log(deletePost);
};

run();
```

### Social Media API NPM 套件影片介紹

<div class="video-container">
  <iframe width="560" height="315" src="https://www.youtube.com/embed/OMtj2h6sW6U" title="Social Media API NPM Package" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />
</div>

透過 API 發布到社群媒體

### Profile Key

你可以在 POST 的請求主體或 GET 的查詢字串中，透過 `profileKey` 欄位指定使用者設定檔的 Profile Key。

### Social API 範例

如需 Node.js 整合範例（使用 RESTful API 呼叫），請參閱 GitHub 儲存庫：

<Card title="Social API 範例程式碼" icon="github" href="https://github.com/ayrshare/social-api-demo" horizontal>
  Social API Demo 是一個 Web 應用程式，讓使用者可以撰寫、排程並同時發布內容到多個社群媒體平台。
</Card>

### 更多資訊與文件

<ul class="custom-bullets">
  <li>[NPM 套件](https://www.npmjs.com/package/social-media-api)</li>
  <li>[Github 儲存庫](https://github.com/ayrshare/social-media-api)</li>
  <li>[問題討論](https://github.com/ayrshare/social-media-api/issues)</li>
</ul>
