# YouTube Search (`youtube.search`)

Keyword search over YouTube videos with relevance or popularity sorting, upload-date filters and cursor pagination.

Docs: https://primaryapi.com/docs/api-reference/youtube/search

- Endpoint: `POST https://api.primaryapi.com/v1/run/youtube.search`
- Category: Social · Platform: `youtube` · Kind: search · Status: live
- Price: 2 credits per call; up to 16 credits if a call is retried elsewhere. A quote’s `maxCredits` is the most a given call can cost.
- Billing unit: per successful call
- Reliability: routed to a healthy upstream and retried automatically when an attempt fails; live 30-day status: `GET https://api.primaryapi.com/v1/status/apis?sku=youtube.search`
- Execution: synchronous (answers 202 only if it outlives the wait window)

## Input

| Field | Type | Required | Default | Constraints | Description |
|---|---|---|---|---|---|
| `query` | string | yes |  | max 500 chars | Search keywords, as typed into the YouTube search box. |
| `sortBy` | enum | no | "relevance" | one of: relevance, popular | Sort order: relevance (default) or popular (most viewed). |
| `uploadDate` | enum | no |  | one of: today, this_week, this_month, this_year | Only videos uploaded today, this week, this month or this year. Omit for any time. |
| `limit` | integer | no | 20 | 1–50 | Maximum number of results to return (1-50, default 20). |
| `cursor` | string | no |  |  | Opaque pagination cursor: pass the previous response's nextCursor. |
| `requireCursor` | boolean | no |  |  | Set true when you intend to page: the call is then served only by sources that return a nextCursor (AnyAPI-compatible flag). |

Unknown fields are rejected with `400 invalid_input`. The routing controls `preferLatencyUnderMs` and `maxCredits` may be sent in the same JSON body.

## Output

When `output.found` is `true`, `output.data` has this shape:

| Field | Type | Description |
|---|---|---|
| `videos` | object[] |  |
| `videos[].id` | string | 11-character video ID |
| `videos[].url` | string | Watch URL |
| `videos[].title` | string \| null |  |
| `videos[].image` | string \| null | Thumbnail URL |
| `videos[].views` | integer \| null |  |
| `videos[].lengthText` | string \| null | Duration label as YouTube shows it, e.g. "4:14" |
| `videos[].durationSeconds` | integer \| null |  |
| `videos[].publishedTime` | string \| null | Relative publish label, e.g. "2 years ago" |
| `videos[].createdUtc` | string \| null | Publish time, ISO 8601 (best effort; null when the source only gives a relative label) |
| `videos[].channel` | string \| null | Channel title |
| `videos[].channelTitle` | string \| null | Channel title (same value as channel; AnyAPI hashtag-search naming) |
| `videos[].authorId` | string \| null | Channel ID (UC…) |
| `videos[].authorHandle` | string \| null | Channel handle without @ |
| `videos[].authorAvatarUrl` | string \| null |  |
| `nextCursor` | string \| null |  |

When the thing you asked about does not exist (or a search matches nothing) the call still succeeds: `output.found` is `false`, `output.data` is `null` and `output.reason` says why (`not_found`, `private`, `no_results`, …). Misses are free unless the notes below say misses are billed.

Pagination: pass the previous response’s `nextCursor` as `cursor` to get the next page; `nextCursor: null` means there are no more results.

## Example request

```bash
curl -sS -X POST "https://api.primaryapi.com/v1/run/youtube.search" \
  -H "Authorization: Bearer $PRIMARYAPI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query":"running shoes review"}'
```

## Example response

_Values are placeholders that follow the output schema; `credits` is what this call would cost at the usual price._

```json
{
  "output": {
    "found": true,
    "data": {
      "videos": [
        {
          "id": "BzSzwqb-OEE",
          "url": "https://www.youtube.com/watch?v=BzSzwqb-OEE",
          "title": "NF - RUNNING (Audio)",
          "image": "https://i.ytimg.com/vi/BzSzwqb-OEE/hq720.jpg",
          "views": 14860541,
          "lengthText": "4:14",
          "durationSeconds": 254,
          "publishedTime": "2 years ago",
          "createdUtc": "2023-05-28T17:08:46.499Z",
          "channel": "NFrealmusic",
          "channelTitle": "NFrealmusic",
          "authorId": "UCoRR6OLuIZ2-5VxtnQIaN2w",
          "authorHandle": null,
          "authorAvatarUrl": "https://yt3.ggpht.com/J1_Si0TYNZ-991v09y8RpCh4_Z_ALwKmPgMYnJqjNhoglVtipf3oEN8LpzG1kS0qsv8Jptpmmg=s88-c-k-c0x00ffffff-no-rj"
        }
      ],
      "nextCursor": "eyJsIjoieW91dHViZS5zZWFyY2gjc2NyYXBlY3JlYXRvcnMiLCJ0IjoiRW9vREVnLi4uIn0"
    }
  },
  "provider": "PrimaryAPI",
  "credits": 2,
  "items": 1,
  "requestId": "req_01JBXK6Y2N9V8T4R3Q2P1M0A7Z",
  "nextCursor": null
}
```

## Pricing and reliability

- Usual price: 2 credits per call
- Price ceiling: up to 16 credits if a call is retried elsewhere. A quote’s `maxCredits` is the most a given call can cost; the `maxCredits` routing control caps it per call
- Live 30-day status, success rate, uptime and latency: `GET https://api.primaryapi.com/v1/status/apis?sku=youtube.search`

## Errors to handle

`invalid_input`, `unauthorized`, `insufficient_balance`, `key_limit_reached`, `trial_cap_reached`, `forbidden`, `not_found`, `idempotency_conflict`, `rate_limited`, `all_sources_failed`, `service_unavailable`, `timeout`. See https://primaryapi.com/docs/errors.

