Skip to content

Commit 3ca3614

Browse files
V2 Media Metata & Updated Docs (#575)
1 parent 54e1ffe commit 3ca3614

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

doc/v2.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ This is a comprehensive guide of all methods available for the Twitter API v2 on
6262
- [Mute someone](#mute-someone)
6363
- [Unmute someone](#unmute-someone)
6464
- [Get users that are muted by you](#get-users-that-are-muted-by-you)
65+
- [Upload medias](#upload-medias)
66+
- [Upload media](#upload-media)
67+
- [Upload media metadata](#upload-media-metadata)
6568
- [Usage](#usage)
6669
- [Get usage](#get-usage)
6770
- [Lists](#lists)
@@ -999,6 +1002,51 @@ for await (const mutedUser of mutedPaginator) {
9991002
}
10001003
```
10011004

1005+
## Upload medias
1006+
1007+
### Upload media
1008+
1009+
**Method**: `.uploadMedia()`
1010+
1011+
**Endpoint**: `media/upload`
1012+
1013+
**Right level**: `Read-write`
1014+
1015+
**Arguments**:
1016+
- `media: Buffer`: Media buffer
1017+
- `options: MediaV2UploadParams`
1018+
- `options.media_type`: EUploadMimeType (the mimetype of the media)
1019+
- `options.media_category?`: MediaV2MediaCategory (the category of the media)
1020+
- `options.chunkSize`: Chunk size
1021+
1022+
**Returns**: `string`: Media ID
1023+
1024+
**Example**
1025+
```ts
1026+
const mediaId = await client.v2.uploadMedia(Buffer.from('imgae.png'), { media_type: 'image/png' });
1027+
const newTweet = await client.v1.tweet('Hello!', { media_ids: mediaId });
1028+
```
1029+
1030+
### Upload media metadata
1031+
1032+
**Method**: `.createMediaMetadata()`
1033+
1034+
**Endpoint**: `media/metadata`
1035+
1036+
**Right level**: `Read-write`
1037+
1038+
**Arguments**:
1039+
- `mediaId: string`
1040+
- `metadata: MediaV2MetadataCreateParams`
1041+
- `metadata.alt_text`: `{ text: string }` the alt text of the media
1042+
1043+
**Returns**: `MediaV2MetadataCreateResult`
1044+
1045+
**Example**
1046+
```ts
1047+
await client.v2.createMediaMetadata(mediaId, { alt_text: { text: 'Hello, world!' } });
1048+
```
1049+
10021050
### Get usage
10031051

10041052
**Method**: `.usage()`

src/types/v2/media.v2.types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ export interface MediaV2UploadResponse {
3636
expires_after_secs: number;
3737
processing_info?: MediaV2ProcessingInfo;
3838
};
39+
}
40+
41+
export interface MediaV2MetadataCreateParams {
42+
alt_text?: { text: string };
43+
}
44+
45+
export interface MediaV2MetadataCreateResult {
46+
data: {
47+
id: string;
48+
associated_metadata: {
49+
alt_text: { text: string };
50+
};
51+
};
3952
}

src/v2/client.v2.write.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {
2424
} from '../types';
2525
import TwitterApiv2LabsReadWrite from '../v2-labs/client.v2.labs.write';
2626
import { CreateDMConversationParams, PostDMInConversationParams, PostDMInConversationResult } from '../types/v2/dm.v2.types';
27-
import { MediaV2MediaCategory, MediaV2UploadAppendParams, MediaV2UploadFinalizeParams, MediaV2UploadInitParams, MediaV2UploadResponse } from '../types/v2/media.v2.types';
27+
import { MediaV2MediaCategory, MediaV2MetadataCreateParams, MediaV2MetadataCreateResult, MediaV2UploadAppendParams, MediaV2UploadFinalizeParams, MediaV2UploadInitParams, MediaV2UploadResponse } from '../types/v2/media.v2.types';
2828

2929
/**
3030
* Base Twitter v2 client with read/write rights.
@@ -215,6 +215,15 @@ export default class TwitterApiv2ReadWrite extends TwitterApiv2ReadOnly {
215215
}
216216
}
217217

218+
/**
219+
* Creates the metadata for media to be uploaded.
220+
* This feature is currently only supported for images and GIFs.
221+
* https://docs.x.com/x-api/media/metadata-create
222+
*/
223+
public createMediaMetadata(mediaId: string, metadata: Partial<MediaV2MetadataCreateParams>) {
224+
return this.post<MediaV2MetadataCreateResult>('/media/metadata', { media_id: mediaId, ...metadata });
225+
}
226+
218227
/**
219228
* Reply to a Tweet on behalf of an authenticated user.
220229
* https://developer.x.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets

0 commit comments

Comments
 (0)