> ## Documentation Index
> Fetch the complete documentation index at: https://generaltranslation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ブックマークの管理

> X API を使用してブックマークを追加および削除します

export const Button = ({href, children}) => {
  return <div className="not-prose group">
    <a href={href}>
      <button className="flex items-center space-x-2.5 py-1 px-4 bg-primary-dark dark:bg-white text-white dark:text-gray-950 rounded-full group-hover:opacity-[0.9] font-medium">
        <span>
          {children}
        </span>
        <svg width="3" height="24" viewBox="0 -9 3 24" class="h-6 rotate-0 overflow-visible"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path></svg>
      </button>
    </a>
  </div>;
};

このガイドでは、X API を使用してブックマークを追加および削除する方法を説明します。

<Note>
  **前提条件**

  作業を始める前に、次のものが必要です。

  * 承認された App を持つ [開発者アカウント](https://developer.x.com/en/portal/petition/essential/basic-info)
  * `bookmark.write` スコープを持つユーザーアクセストークン (OAuth 2.0 PKCE)
</Note>

***

<div id="add-a-bookmark">
  ## ブックマークを追加する
</div>

<Steps>
  <Step title="ユーザーIDを取得する">
    認証済みユーザーのIDが必要です。`/2/users/me` エンドポイント、または [ユーザー lookup エンドポイント](/ja/x-api/users/lookup/introduction)を使って取得できます。
  </Step>

  <Step title="ポストIDを取得する">
    ポストを表示しているときのURLからポストIDを確認します：

    ```
    https://x.com/XDevelopers/status/1460323737035677698
                                    └── これがポストIDです
    ```
  </Step>

  <Step title="ブックマークを追加する">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/users/2244994945/bookmarks" \
        -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{"tweet_id": "1460323737035677698"}'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

      # ブックマークを追加する
      response = client.bookmarks.create(
          user_id="2244994945",
          tweet_id="1460323737035677698"
      )

      print(f"Bookmarked: {response.data.bookmarked}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

      // ブックマークを追加する
      const response = await client.bookmarks.create("2244994945", {
        tweetId: "1460323737035677698",
      });

      console.log(`Bookmarked: ${response.data?.bookmarked}`);
      ```
    </CodeGroup>
  </Step>

  <Step title="レスポンスを確認する">
    ```json theme={null}
    {
      "data": {
        "bookmarked": true
      }
    }
    ```
  </Step>
</Steps>

***

<div id="remove-a-bookmark">
  ## ブックマークを削除する
</div>

ブックマークからポストを削除するには、次のようにします:

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl -X DELETE "https://api.x.com/2/users/2244994945/bookmarks/1460323737035677698" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN"
  ```

  ```python Python SDK theme={null}
  from xdk import Client

  client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

  # ブックマークを削除する
  response = client.bookmarks.delete(
      user_id="2244994945",
      tweet_id="1460323737035677698"
  )

  print(f"Bookmarked: {response.data.bookmarked}")
  ```

  ```javascript JavaScript SDK theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

  // ブックマークを削除する
  const response = await client.bookmarks.delete("2244994945", "1460323737035677698");

  console.log(`Bookmarked: ${response.data?.bookmarked}`);
  ```
</CodeGroup>

**レスポンス:**

```json theme={null}
{
  "data": {
    "bookmarked": false
  }
}
```

***

<div id="required-scopes">
  ## 必要なスコープ
</div>

OAuth 2.0 PKCE を使用する場合、アクセストークンには次のスコープが必要です。

| Scope            | Description  |
| :--------------- | :----------- |
| `bookmark.write` | ブックマークの追加と削除 |
| `tweet.read`     | ポストのデータの読み取り |
| `users.read`     | ユーザーデータの読み取り |

***

<div id="next-steps">
  ## 次のステップ
</div>

<CardGroup cols={2}>
  <Card title="ブックマークの取得" icon="bookmark" href="/ja/x-api/posts/bookmarks/quickstart/bookmarks-lookup">
    ブックマークした投稿を取得
  </Card>

  <Card title="APIリファレンス" icon="code" href="/ja/x-api/posts/add-post-to-bookmarks">
    エンドポイントの完全なドキュメント
  </Card>
</CardGroup>
