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

# 最初のリクエストを送信する

> 最初の X Enterprise API リクエストを送信します。認証セットアップ、エンドポイントの選択、cURL コマンド例、レスポンス処理のヒントを含みます。

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 リクエストを送信する手順を説明します。開始する前に、[アプリ認証情報を含むデベロッパーアカウント](/x-api/getting-started/getting-access) が必要です。

***

## cURL でクイックスタート

API をテストする最も簡単な方法は cURL です。ユーザーを検索してみましょう:

```bash theme={null}
curl "https://api.x.com/2/users/by/username/xdevelopers" \
  -H "Authorization: Bearer $BEARER_TOKEN"
```

`$BEARER_TOKEN` を実際の Bearer Token に置き換えてください。次のようなレスポンスが返されます:

```json theme={null}
{
  "data": {
    "id": "2244994945",
    "name": "X Developers",
    "username": "xdevelopers"
  }
}
```

***

## ステップバイステップガイド

<Steps>
  <Step title="Bearer Token を取得する">
    [Developer Console](https://console.x.com) でアプリに移動し、Bearer Token をコピーします。
  </Step>

  <Step title="エンドポイントを選択する">
    まずは初心者向けの次のエンドポイントから始めましょう:

    | エンドポイント                                           | 機能                         |
    | :------------------------------------------------ | :------------------------- |
    | [User lookup](/x-api/users/lookup/introduction)   | ユーザー名または ID でユーザープロフィールを取得 |
    | [Post lookup](/x-api/posts/lookup/introduction)   | ID で投稿を取得                  |
    | [Recent search](/x-api/posts/search/introduction) | 過去 7 日間の投稿を検索              |
  </Step>

  <Step title="リクエストを送信する">
    cURL、Postman、またはお好みの HTTP クライアントを使用します:

    ```bash theme={null}
    # ユーザー名でユーザーを検索
    curl "https://api.x.com/2/users/by/username/xdevelopers" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Step>

  <Step title="レスポンスを解析する">
    レスポンスは JSON 形式です。主要なデータは `data` フィールドに含まれます:

    ```json theme={null}
    {
      "data": {
        "id": "2244994945",
        "name": "X Developers",
        "username": "xdevelopers"
      }
    }
    ```
  </Step>
</Steps>

***

## fields でより多くのデータをリクエストする

デフォルトでは、エンドポイントは最小限のフィールドのみを返します。`fields` パラメータを使用して追加データをリクエストできます:

```bash theme={null}
curl "https://api.x.com/2/users/by/username/xdevelopers?user.fields=created_at,description,public_metrics" \
  -H "Authorization: Bearer $BEARER_TOKEN"
```

レスポンス:

```json theme={null}
{
  "data": {
    "id": "2244994945",
    "name": "X Developers",
    "username": "xdevelopers",
    "created_at": "2013-12-14T04:35:55.000Z",
    "description": "The voice of the X Developer Platform",
    "public_metrics": {
      "followers_count": 570842,
      "following_count": 2048,
      "tweet_count": 14052,
      "listed_count": 1672
    }
  }
}
```

[fields の詳細はこちら →](/x-api/fundamentals/fields)

***

## その他の例

<Tabs>
  <Tab title="投稿を検索する">
    ```bash theme={null}
    curl "https://api.x.com/2/tweets/1460323737035677698?tweet.fields=created_at,public_metrics" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>

  <Tab title="最近の投稿を検索する">
    ```bash theme={null}
    curl "https://api.x.com/2/tweets/search/recent?query=from:xdevelopers&tweet.fields=created_at" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>

  <Tab title="ユーザーの投稿を取得する">
    ```bash theme={null}
    curl "https://api.x.com/2/users/2244994945/tweets?max_results=5" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```
  </Tab>
</Tabs>

***

## cURL の代わりにコードを使う

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import requests

    bearer_token = "YOUR_BEARER_TOKEN"
    url = "https://api.x.com/2/users/by/username/xdevelopers"

    headers = {"Authorization": f"Bearer {bearer_token}"}
    response = requests.get(url, headers=headers)

    print(response.json())
    ```
  </Tab>

  <Tab title="JavaScript">
    ```javascript theme={null}
    const bearerToken = "YOUR_BEARER_TOKEN";
    const url = "https://api.x.com/2/users/by/username/xdevelopers";

    fetch(url, {
      headers: { Authorization: `Bearer ${bearerToken}` }
    })
      .then(res => res.json())
      .then(data => console.log(data));
    ```
  </Tab>

  <Tab title="公式 SDK">
    本番利用には、公式 SDK の使用をおすすめします:

    * [Python SDK](/xdks/python/overview)
    * [TypeScript SDK](/xdks/typescript/overview)

    これらは認証、ページネーション、レート制限を自動的に処理します。
  </Tab>
</Tabs>

***

## テスト用ツール

<CardGroup cols={3}>
  <Card title="Postman" icon="server" href="/tutorials/postman-getting-started">
    コレクションを使ったビジュアル API テスト。
  </Card>

  <Card title="サンプルコード" icon="github" href="https://github.com/xdevplatform/Twitter-API-v2-sample-code">
    複数の言語での例。
  </Card>

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

***

## トラブルシューティング

<Accordion title="401 Unauthorized">
  * Bearer Token が正しいか確認してください
  * トークンが再生成されていないことを確認してください
  * `Authorization` ヘッダーの形式を確認: `Bearer YOUR_TOKEN`
</Accordion>

<Accordion title="403 Forbidden">
  * アプリがこのエンドポイントへのアクセス権を持っていない可能性があります
  * 一部のエンドポイントはユーザーコンテキスト認証 (OAuth 1.0a または 2.0) が必要です
  * Developer Console でアプリの権限を確認してください
</Accordion>

<Accordion title="429 Too Many Requests">
  * レート制限に達しました
  * 再試行のタイミングについては `x-rate-limit-reset` ヘッダーを確認してください
  * コードに指数バックオフを実装してください
</Accordion>

[完全なエラーリファレンス →](/x-api/fundamentals/response-codes-and-errors)

***

## 次のステップ

<CardGroup cols={2}>
  <Card title="認証を学ぶ" icon="key" href="/resources/fundamentals/authentication/overview">
    ユーザーコンテキストリクエストのための OAuth を理解しましょう。
  </Card>

  <Card title="エンドポイントを探索する" icon="compass" href="/x-api/posts/search/introduction">
    何が構築できるか発見しましょう。
  </Card>

  <Card title="SDK を使う" icon="cube" href="/tools-and-libraries">
    公式ライブラリで開発を加速。
  </Card>

  <Card title="何かを作る" icon="hammer" href="/x-api/what-to-build">
    作るもののアイデア。
  </Card>
</CardGroup>
