---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://docs.sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://docs.sendcorex.com/llms.txt"
---

Source: https://docs.sendcorex.com/ja/api-reference/contacts/segments/run-segment

連絡先：セグメント

# セグメントの実行

保存したセグメントに今一致する連絡先を取得します。出力とページネーションはQuery Contactsと同じです。

POST `https://g.sendcorex.com/v1/contacts` command: run_segment

API keyに`contact:read`スコープが必要です。

## 引数

- `segment_id` string 必須
実行するセグメント。
例： `"engaged-vips"`
- `limit` number 任意
ページサイズ（1〜1000）。デフォルトは50。
- `cursor` string 任意
前のページの`next_cursor`。
- `select` string[] 任意
各連絡先のこれらの部分だけを返します（例：`["status", "fields.plan", "stats.open_count"]`）。`email`は常に返ります。指定しないと連絡先全体が返ります。

## クイックスタート

```
import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1/contacts',
  {
    commands: [
      {
        command: "run_segment",
        args: {
          segment_id: "engaged-vips",
          select: [
            "first_name",
            "stats.open_rate"
          ],
          limit: 500
        }
      }
    ]
  },
  { headers: { Authorization: process.env.SENDCOREX_API_KEY } },
);

const [result] = response.data.results;
if (result.type !== 'SUCCESS') throw new Error(`${result.code}: ${result.message}`);
console.log(result.data);
```

## レスポンス

```
{
  "type": "SUCCESS",
  "results": [
    {
      "command": "run_segment",
      "id": "0",
      "type": "SUCCESS",
      "took_ms": 14,
      "data": {
        "contacts": [
          {
            "email": "ana@acme.com",
            "first_name": "Ana",
            "stats": {
              "open_rate": 0.58
            }
          }
        ],
        "has_more": false,
        "next_cursor": null,
        "plan": {
          "driver": "tag:vip",
          "examined": 412,
          "keys_only": false,
          "ms": {
            "scan": 4,
            "fetch": 9,
            "filter": 1,
            "total": 14
          }
        }
      }
    }
  ]
}
```

### レスポンスの項目

- `contacts`
一致した連絡先。連絡先全体、または`select`で指定した部分のみ。
- `has_more / next_cursor`
ページネーション。`has_more`が`true`の間は、同じ`where`と`sort`で`next_cursor`を`cursor`として送ります。
- `plan.driver`
サーバーが走査に選んだインデックス（例：`tag:vip`、`list:customers`）。
- `plan.examined`
このページを埋めるために読み取ったインデックスのエントリー数。
- `plan.keys_only`
連絡先を読み込まずにインデックスだけで返した場合は`true`。
- `plan.ms`
走査、取得、絞り込み、合計にかかった時間（ミリ秒）。

## エラー

HTTPレベルのエラーは`type`、`code`、`message`を返します。CMDと表示されたエラーは、HTTP 200でコマンドの結果内に返ります。

- 401`INVALID_API_KEY`
API keyが存在しないか、取り消されたか、ダッシュボードで無効化されています。
- 429`RATE_LIMITED`
同じIP + API keyで毎秒15リクエストまたは同時15件を超えました。Retry-Afterの秒数だけ待ってください。
- CMD`MISSING_SCOPE`
API keyにこのコマンドに必要なスコープがありません。リクエスト内のほかのコマンドは実行されます。
- CMD`INVALID_ARGS`
必須の引数がないか不正です：メールアドレス、項目の値、enumの選択肢、whereのキー、リストのid…
- CMD`NOT_FOUND`
連絡先、リスト、項目、またはセグメントが存在しません。
- CMD`INVALID_CURSOR`
カーソルが不正か、別のクエリまたはアカウントのものです。
- CMD`INTERNAL`
サーバーエラー。詳細はサーバーのログにのみ記録されます。
