> ## 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 Dashboard](https://app.ayrshare.com/) 中获取你的密钥 API Key。</Info>

### 一般用法

发帖（Post）、历史记录（History）和删除（Delete）的示例：

<Info>
  **发布到 X/Twitter？** 自 2026 年 3 月 31 日起，通过 Ayrshare 进行 X/Twitter 的操作需要使用你自己的 X Developer App 凭据——Ayrshare 会在每个 X 相关调用上强制执行这一点。请在请求中添加两个 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"; // 在 ayrshare.com 获取 API Key
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" // 与用户配置一起使用
    })
    .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

你可以通过 `profileKey` 字段在 POST 请求的请求体或 GET 请求的查询参数中指定用户配置的 Profile Key。

### Social API Demo

参考 GitHub 仓库获取一个使用 RESTful API 调用的 Node.js 示例集成：

<Card title="Social API Demo Code" 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>[Issue 讨论](https://github.com/ayrshare/social-media-api/issues)</li>
</ul>
