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

# Recent Search クイックスタート

> このガイドでは、直近 7 日間の投稿を検索する初回の recent search リクエストの手順を説明します。クイックスタートを扱う X API v2 スタンダードティアのリファレンス。

export const Button = ({href, children}) => {
  return <div className="not-prose">
    <a href={href}>
      <button className="x-btn">
        <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>;
};

このガイドでは、直近 7 日間の投稿を検索する初回の recent search リクエストの手順を説明します。

<Note>
  **前提条件**

  始める前に以下が必要です:

  * 承認済みの App を含む[開発者アカウント](https://developer.x.com/en/portal/petition/essential/basic-info)
  * App の Bearer Token(Developer Console の「Keys and tokens」で確認できます)
</Note>

***

<Steps>
  <Step title="クエリを組み立てる" icon="https://mintcdn.com/x-preview/cfyQtgCdwk8p69aa/icons/xds/icon-search.svg?fit=max&auto=format&n=cfyQtgCdwk8p69aa&q=85&s=8c11ad89387b7c09ced1553d5c232834" width="24" height="24" data-path="icons/xds/icon-search.svg">
    検索クエリは演算子を使って投稿にマッチします。シンプルなキーワードから始めます:

    ```
    python
    ```

    または複数の演算子を組み合わせます:

    ```
    python lang:en -is:retweet
    ```

    これは、リツイートを除く、英語で "python" を含む投稿にマッチします。

    <Tip>
      利用可能なすべてのオプションは[完全な演算子リファレンス](/x-api/posts/search/integrate/operators)を参照してください。
    </Tip>
  </Step>

  <Step title="リクエストを実行する" icon="terminal">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl "https://api.x.com/2/tweets/search/recent?query=python%20lang%3Aen%20-is%3Aretweet" \
        -H "Authorization: Bearer $BEARER_TOKEN"
      ```

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

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # Recent Posts を検索
      for page in client.posts.search_recent(
          query="python lang:en -is:retweet"
      ):
          for post in page.data:
              print(post.text)
      ```

      ```javascript title="JavaScript SDK" lines wrap icon="square-js" theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // Recent Posts を検索
      const paginator = client.posts.searchRecent({
        query: "python lang:en -is:retweet",
      });

      for await (const page of paginator) {
        page.data?.forEach((post) => {
          console.log(post.text);
        });
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="レスポンスを確認する" icon="eye">
    デフォルトレスポンスには `id`、`text`、`edit_history_tweet_ids` が含まれます:

    ```json theme={null}
    {
      "data": [
        {
          "id": "1234567890123456789",
          "text": "Just started learning Python and loving it!",
          "edit_history_tweet_ids": ["1234567890123456789"]
        },
        {
          "id": "1234567890123456788",
          "text": "Python tip: use list comprehensions for cleaner code",
          "edit_history_tweet_ids": ["1234567890123456788"]
        }
      ],
      "meta": {
        "newest_id": "1234567890123456789",
        "oldest_id": "1234567890123456788",
        "result_count": 2
      }
    }
    ```
  </Step>

  <Step title="fields と expansions を追加する" icon="sliders">
    クエリパラメーターで追加データをリクエストします:

    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl "https://api.x.com/2/tweets/search/recent?\
      query=python%20lang%3Aen%20-is%3Aretweet&\
      tweet.fields=created_at,public_metrics,author_id&\
      expansions=author_id&\
      user.fields=username,verified&\
      max_results=10" \
        -H "Authorization: Bearer $BEARER_TOKEN"
      ```

      ```python title="Python SDK" lines wrap icon="python" theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_BEARER_TOKEN")

      # fields と expansions を指定して検索
      for page in client.posts.search_recent(
          query="python lang:en -is:retweet",
          tweet_fields=["created_at", "public_metrics", "author_id"],
          expansions=["author_id"],
          user_fields=["username", "verified"],
          max_results=10
      ):
          for post in page.data:
              print(f"{post.text[:50]}... - Likes: {post.public_metrics.like_count}")
      ```

      ```javascript title="JavaScript SDK" lines wrap icon="square-js" theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ bearerToken: "YOUR_BEARER_TOKEN" });

      // fields と expansions を指定して検索
      const paginator = client.posts.searchRecent({
        query: "python lang:en -is:retweet",
        tweetFields: ["created_at", "public_metrics", "author_id"],
        expansions: ["author_id"],
        userFields: ["username", "verified"],
        maxResults: 10,
      });

      for await (const page of paginator) {
        page.data?.forEach((post) => {
          console.log(`${post.text?.slice(0, 50)}... - Likes: ${post.public_metrics?.like_count}`);
        });
      }
      ```
    </CodeGroup>

    **レスポンス:**

    ```json title="レスポンス例" expandable lines wrap icon="https://mintcdn.com/x-preview/Vn2KEkZaPF9LiPi3/icons/xds/icon-brackets.svg?fit=max&auto=format&n=Vn2KEkZaPF9LiPi3&q=85&s=ed2428e77bab43e57800e1a590e982fa" theme={null}
    {
      "data": [
        {
          "id": "1234567890123456789",
          "text": "Just started learning Python and loving it!",
          "created_at": "2024-01-15T10:30:00.000Z",
          "author_id": "9876543210",
          "public_metrics": {
            "retweet_count": 5,
            "reply_count": 2,
            "like_count": 42,
            "quote_count": 1
          },
          "edit_history_tweet_ids": ["1234567890123456789"]
        }
      ],
      "includes": {
        "users": [
          {
            "id": "9876543210",
            "username": "pythondev",
            "verified": false
          }
        ]
      },
      "meta": {
        "newest_id": "1234567890123456789",
        "oldest_id": "1234567890123456789",
        "result_count": 1
      }
    }
    ```
  </Step>

  <Step title="結果をページネーションする" icon="https://mintcdn.com/x-preview/Vn2KEkZaPF9LiPi3/icons/xds/icon-arrow-right.svg?fit=max&auto=format&n=Vn2KEkZaPF9LiPi3&q=85&s=88e933002782dbdeb204043cedef033e" width="24" height="24" data-path="icons/xds/icon-arrow-right.svg">
    SDK はページネーションを自動的に処理します。cURL の場合、レスポンスの `next_token` を使用します:

    ```bash theme={null}
    curl "https://api.x.com/2/tweets/search/recent?\
    query=python&\
    max_results=100&\
    next_token=b26v89c19zqg8o3fo7gesq314yb9l2l4ptqy" \
      -H "Authorization: Bearer $BEARER_TOKEN"
    ```

    <Card title="ページネーションガイド" icon="https://mintcdn.com/x-preview/Vn2KEkZaPF9LiPi3/icons/xds/icon-arrow-right.svg?fit=max&auto=format&n=Vn2KEkZaPF9LiPi3&q=85&s=88e933002782dbdeb204043cedef033e" href="/x-api/posts/search/integrate/paginate" width="24" height="24" data-path="icons/xds/icon-arrow-right.svg">
      大規模な結果セットの移動について詳しく学ぶ
    </Card>
  </Step>
</Steps>

***

## クエリ例

<AccordionGroup>
  <Accordion title="特定ユーザーの投稿">
    ```
    from:XDevelopers
    ```
  </Accordion>

  <Accordion title="ハッシュタグ付きの投稿">
    ```
    #Python -is:retweet
    ```
  </Accordion>

  <Accordion title="画像付きの投稿">
    ```
    "machine learning" has:images lang:en
    ```
  </Accordion>

  <Accordion title="ユーザーをメンションする投稿">
    ```
    @elonmusk -is:retweet -is:reply
    ```
  </Accordion>

  <Accordion title="ドメインへのリンクを含む投稿">
    ```
    url:github.com lang:en
    ```
  </Accordion>
</AccordionGroup>

***

## 次のステップ

<CardGroup cols={2}>
  <Card title="クエリを組み立てる" icon="https://mintcdn.com/x-preview/cfyQtgCdwk8p69aa/icons/xds/icon-search.svg?fit=max&auto=format&n=cfyQtgCdwk8p69aa&q=85&s=8c11ad89387b7c09ced1553d5c232834" href="/x-api/posts/search/integrate/build-a-query" width="24" height="24" data-path="icons/xds/icon-search.svg">
    クエリ構文と演算子を習得
  </Card>

  <Card title="演算子リファレンス" icon="list-check" href="/x-api/posts/search/integrate/operators">
    利用可能なすべての演算子を確認
  </Card>

  <Card title="Full-archive search" icon="vault" href="/x-api/posts/search/quickstart/full-archive-search">
    投稿の完全アーカイブを検索
  </Card>

  <Card title="API リファレンス" icon="https://mintcdn.com/x-preview/ygI6sSJPehlc0qNT/icons/xds/icon-code.svg?fit=max&auto=format&n=ygI6sSJPehlc0qNT&q=85&s=488e23401b19225b89acc0136d242219" href="/x-api/posts/search-recent-posts" width="24" height="24" data-path="icons/xds/icon-code.svg">
    エンドポイントの完全なドキュメント
  </Card>
</CardGroup>
