> ## 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.

# MediaClient

> Methods for X API v2 Media endpoints.

Access **Media** endpoints via `client.media` (`MediaClient`).

## Methods

| Method | Endpoint | Behavior | Summary |
| :- | :- | :- | :- |
| [`add_post_to_amplify_programs`](#add_post_to_amplify_programs) | `POST /2/media/amplify/posts` | Standard | Add Post to Amplify Programs |
| [`add_to_library`](#add_to_library) | `POST /2/media/library` | Standard | Add Media to Library |
| [`append_upload`](#append_upload) | `POST /2/media/upload/{id}/append` | Standard | Append Media Upload |
| [`create_metadata`](#create_metadata) | `POST /2/media/metadata` | Standard | Create Media Metadata |
| [`create_subtitles`](#create_subtitles) | `POST /2/media/subtitles` | Standard | Create Media Subtitles |
| [`delete_subtitles`](#delete_subtitles) | `DELETE /2/media/subtitles` | Standard | Delete Media Subtitles |
| [`finalize_upload`](#finalize_upload) | `POST /2/media/upload/{id}/finalize` | Standard | Finalize Media Upload |
| [`get_amplify_program`](#get_amplify_program) | `GET /2/media/amplify/programs/{program_id}` | Standard | Get Amplify Program |
| [`get_amplify_program_posts`](#get_amplify_program_posts) | `GET /2/media/amplify/programs/{program_id}/posts` | Standard | Get Amplify Program Posts |
| [`get_amplify_programs`](#get_amplify_programs) | `GET /2/media/amplify/programs` | Standard | Get Amplify Programs |
| [`get_analytics`](#get_analytics) | `GET /2/media/analytics` | Standard | Get Media Analytics |
| [`get_by_key`](#get_by_key) | `GET /2/media/{media_key}` | Standard | Get Media by Media Key |
| [`get_by_keys`](#get_by_keys) | `GET /2/media` | Standard | Get Media by Media Keys |
| [`get_library_categories`](#get_library_categories) | `GET /2/media/library/categories` | Standard | Get Media Library Categories |
| [`get_upload_status`](#get_upload_status) | `GET /2/media/upload` | Standard | Get Media Upload Status |
| [`initialize_upload`](#initialize_upload) | `POST /2/media/upload/initialize` | Standard | Initialize Media Upload |
| [`remove_from_library`](#remove_from_library) | `DELETE /2/media/library/{media_key}` | Standard | Remove Media Library Item |
| [`update_in_library`](#update_in_library) | `PUT /2/media/library/{media_key}` | Standard | Update Media Library Item |
| [`upload`](#upload) | `POST /2/media/upload` | Standard | Upload Media |

## `add_post_to_amplify_programs`

POST `/2/media/amplify/posts`

**Add Post to Amplify Programs**

Publishes one of the authenticated user's video posts into one or more active Amplify pre-roll programs the user is enrolled in as a publisher.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.add_post_to_amplify_programs(
  body: { "post_id" => "1895000000000000000", "program_ids" => ["1895000000000000000"] }
)
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `add_to_library`

POST `/2/media/library`

**Add Media to Library**

Adds previously uploaded media to the authenticated user's Media Library with its library metadata. The user must own the media. Geo restrictions, content expiry, the preview image and the audience policy are set with the Media metadata endpoint.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.add_to_library(body: { "media_key" => "3_1895000000000000000" })
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `append_upload`

POST `/2/media/upload/{id}/append`

**Append Media Upload**

Appends one segment of the file to a chunked upload. Send the segment base64-encoded in media (raw bytes in multipart bodies) and its position in the file in segment\_index, counting from 0. A segment can be up to 8 MB; in a JSON body keep it to 4 MB or less, because base64 adds a third. After the last segment, call Finalize Media Upload.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.append_upload(
  id: "1895000000000000000",
  body: { "media" => "example", "segment_index" => 1 }
)
puts response
```

### Parameters

**id** `String` *required*

The upload id from Initialize Media Upload.

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `create_metadata`

POST `/2/media/metadata`

**Create Media Metadata**

Creates metadata for a Media file.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.create_metadata(
  body: { "id" => "1895000000000000000", "metadata" => { "allow_download_status" => { "allow_download" => "example" } } }
)
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `create_subtitles`

POST `/2/media/subtitles`

**Create Media Subtitles**

Creates subtitles for a specific Media file.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.create_subtitles(
  body: { "id" => "1895000000000000000", "media_category" => "AmplifyVideo", "subtitles" => { "display_name" => "example" } }
)
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `delete_subtitles`

DELETE `/2/media/subtitles`

**Delete Media Subtitles**

Deletes subtitles for a specific Media file.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.delete_subtitles(
  body: { "id" => "1895000000000000000", "media_category" => "example", "language_code" => "example" }
)
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `finalize_upload`

POST `/2/media/upload/{id}/finalize`

**Finalize Media Upload**

Completes a chunked upload once every segment is appended. For video the result carries processing\_info: the media can go on a Post only when its state is succeeded, so call Get Media Upload Status until it is. A result without processing\_info is ready to use.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.finalize_upload(id: "1895000000000000000")
puts response
```

### Parameters

**id** `String` *required*

The upload id from Initialize Media Upload.

**Returns:** `Hash`

***

## `get_amplify_program`

GET `/2/media/amplify/programs/{program_id}`

**Get Amplify Program**

Returns one Amplify pre-roll program the authenticated user is enrolled in as a publisher. A program the user is not enrolled in is reported as not found.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_amplify_program(program_id: "1895000000000000000")
puts response
```

### Parameters

**program\_id** `String` *required*

Identifier of the program.

**Returns:** `Hash`

***

## `get_amplify_program_posts`

GET `/2/media/amplify/programs/{program_id}/posts`

**Get Amplify Program Posts**

Returns the posts published into an Amplify pre-roll program the authenticated user is enrolled in as a publisher.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_amplify_program_posts(program_id: "1895000000000000000")
puts response
```

### Parameters

**program\_id** `String` *required*

Identifier of the program.

**Returns:** `Hash`

***

## `get_amplify_programs`

GET `/2/media/amplify/programs`

**Get Amplify Programs**

Returns the Amplify pre-roll programs the authenticated user is enrolled in as a publisher.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_amplify_programs
puts response
```

**Returns:** `Hash`

***

## `get_analytics`

GET `/2/media/analytics`

**Get Media Analytics**

Retrieves analytics data for media.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_analytics(
  media_keys: ["3_1895000000000000000"],
  start_time: "2026-01-01T00:00:00Z",
  end_time: "2026-01-02T00:00:00Z"
)
puts response
```

### Parameters

**media\_keys** `Array\<String\>` *required*

`media_keys` (query parameter).

**start\_time** `String` *required*

`start_time` (query parameter).

**end\_time** `String` *required*

`end_time` (query parameter).

**Optional keyword arguments (2)**

**granularity** `String`

`granularity` (query parameter).

**media\_analytics\_fields** `Array\<String\>`

A comma separated list of MediaAnalytics fields to display.

**Returns:** `Hash`

***

## `get_by_key`

GET `/2/media/{media_key}`

**Get Media by Media Key**

Retrieves details of a specific Media file by its media key.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(bearer_token: ENV.fetch("X_BEARER_TOKEN"))

response = client.media.get_by_key(media_key: "3_1895000000000000000")
puts response
```

### Parameters

**media\_key** `String` *required*

`media_key` (path parameter).

**Optional keyword arguments (1)**

**media\_fields** `Array\<String\>`

A comma separated list of Media fields to display.

**Returns:** `Hash`

***

## `get_by_keys`

GET `/2/media`

**Get Media by Media Keys**

Retrieves details of Media files by their media keys.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(bearer_token: ENV.fetch("X_BEARER_TOKEN"))

response = client.media.get_by_keys(media_keys: ["3_1895000000000000000"])
puts response
```

### Parameters

**media\_keys** `Array\<String\>` *required*

`media_keys` (query parameter).

**Optional keyword arguments (1)**

**media\_fields** `Array\<String\>`

A comma separated list of Media fields to display.

**Returns:** `Hash`

***

## `get_library_categories`

GET `/2/media/library/categories`

**Get Media Library Categories**

Returns the video categories a publisher can assign to a media library item and the advertiser categories whose ads can be blocked from running against it.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_library_categories
puts response
```

### Parameters

**Optional keyword arguments (1)**

**lang** `String`

Language tag for category descriptions, e.g. `en` or `pt-BR`.

**Returns:** `Hash`

***

## `get_upload_status`

GET `/2/media/upload`

**Get Media Upload Status**

Returns how far X has processed an uploaded media file, in processing\_info.state: pending, in\_progress, succeeded or failed. Call it after Finalize Media Upload returns processing\_info, waiting check\_after\_secs between calls, until the state is succeeded. A failed state means the media can't be used.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.get_upload_status(media_id: "3_1895000000000000000")
puts response
```

### Parameters

**media\_id** `String` *required*

The media id from Initialize Media Upload.

**Optional keyword arguments (1)**

**command** `String`

Always STATUS; can be left out.

**Returns:** `Hash`

***

## `initialize_upload`

POST `/2/media/upload/initialize`

**Initialize Media Upload**

Starts a chunked media upload and returns its id. Use it for video and for other files too large for one request. For a video on a Post, set media\_category to tweet\_video, media\_type to the file's type (for example video/mp4) and total\_bytes to the file's size. Send the file with Append Media Upload, call Finalize Media Upload, then pass the id in media.media\_ids on Create or Edit Post.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.initialize_upload(body: { "additional_owners" => ["example"] })
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `remove_from_library`

DELETE `/2/media/library/{media_key}`

**Remove Media Library Item**

Removes an item from the authenticated user's Media Library. Posts scheduled through the API that have not yet been published and still use the media are cancelled. Published posts and the underlying media are not affected.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.remove_from_library(media_key: "3_1895000000000000000", body: {  })
puts response
```

### Parameters

**media\_key** `String` *required*

The media key of the library item to remove.

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `update_in_library`

PUT `/2/media/library/{media_key}`

**Update Media Library Item**

Updates the library metadata of an item in the authenticated user's Media Library. Only the fields sent are changed; the others keep their values. Geo restrictions, content expiry, the preview image and the audience policy are set with the Media metadata endpoint.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.update_in_library(media_key: "3_1895000000000000000", body: { "name" => "My List" })
puts response
```

### Parameters

**media\_key** `String` *required*

The media key of the library item to update.

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***

## `upload`

POST `/2/media/upload`

**Upload Media**

Uploads a media file in one request and returns its id. Send the file base64-encoded in media (raw bytes in multipart bodies). For a Post, set media\_category to tweet\_image (JPG, PNG, GIF or WEBP, up to 5 MB) or tweet\_gif, then pass the returned id in media.media\_ids on Create or Edit Post. DM media uses dm\_image or dm\_gif. For video, use Initialize Media Upload.

```ruby theme={null}
require "xdk"

client = Xdk::Client.new(access_token: ENV.fetch("X_ACCESS_TOKEN"))

response = client.media.upload(body: { "media" => "example", "media_category" => "tweet_image" })
puts response
```

### Parameters

**body** `Hash` *required*

Request payload.

**Returns:** `Hash`

***
