> ## 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.

# Python PyPI

> Ayrshare 的 Python PyPI 用戶端套件

## 概觀

Ayrshare 的 [Social-Post-API PyPI 套件](https://pypi.org/project/social-post-api/) 讓你可以將 Ayrshare API 整合到 Python 應用程式中。

### 安裝

如果你在伺服器端使用 Python，請安裝 [Social-Post-API PyPI 套件](https://pypi.org/project/social-post-api/)。該套件封裝了 RESTful 呼叫，簡化了呼叫方式。

首先在 [Ayrshare 儀表板](https://app.ayrshare.com/api) 取得你的密鑰 API Key。

接著安裝 Python 套件：

```bash theme={"system"}
pip install social-post-api
```

### 一般使用方式

Post、History 與 Delete 的範例。更多資訊請參閱 [PyPI 套件](https://pypi.org/project/social-post-api/)。

<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 內建 `set_twitter_byo(api_key, api_secret)` 輔助函式，會為之後每個請求附加兩個必要的 `X-Twitter-OAuth1-*` 標頭：

```python theme={"system"}
from ayrshare import SocialPost

social = SocialPost(API_KEY)
social.set_twitter_byo(MY_X_API_KEY, MY_X_API_SECRET)

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

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

```python theme={"system"}
social.clear_twitter_byo().set_twitter_byo(next_tenant_key, next_tenant_secret)
```

```python theme={"system"}
from ayrshare import SocialPost
social = SocialPost('8jKj782Aw8910dCN') # get an API Key at ayrshare.com

# Required for any post that includes 'twitter' in platforms (BYO is enforced).
social.set_twitter_byo('YOUR_X_CONSUMER_KEY', 'YOUR_X_CONSUMER_SECRET')

# Post to Platforms Twitter, Facebook, and LinkedIn
postResult = social.post({'post': 'Nice Posting 2', 'platforms': ['twitter', 'facebook', 'linkedin'], 'profileKey': 'JKSDJI-JKKJKKJ'})
print(postResult)

# Delete (use the top-level Ayrshare post id from postResult['id'])
deleteResult = social.delete({'id': postResult['id']})
print(deleteResult)

# History
print(social.history())
```

### Profile Key

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

### 更多資訊與文件

<ul class="custom-bullets">
  <li>[PyPI 套件](https://pypi.org/project/social-post-api/)</li>
  <li>[Github 儲存庫](https://github.com/ayrshare/social-post-api-python)</li>
  <li>[問題討論](https://github.com/ayrshare/social-post-api-python/issues)</li>
</ul>
