Authentication
DM endpoints require user authentication:| Method | Description |
|---|---|
| OAuth 2.0 Authorization Code with PKCE | Recommended |
| OAuth 1.0a User Context | Legacy support |
Required scopes (OAuth 2.0)
| Scope | Required for |
|---|---|
dm.write | Sending and deleting messages |
dm.read | Required with dm.write |
tweet.read | Required with dm scopes |
users.read | Required with dm scopes |
Endpoints overview
| Method | Endpoint | Description |
|---|---|---|
| POST | /2/dm_conversations/with/:participant_id/messages | Send one-to-one message |
| POST | /2/dm_conversations | Create group conversation |
| POST | /2/dm_conversations/:dm_conversation_id/messages | Add message to conversation |
| DELETE | /2/dm_events/:event_id | Delete a message |
Sending messages
One-to-one message
Send a message to a specific user. Creates a new conversation if one doesn’t exist:cURL
Group conversation
Create a new group and send the first message:cURL
The
conversation_type field must be set to "Group" (case sensitive).Add to existing conversation
Send a message to any conversation you’re part of:cURL
Media attachments
Attach one piece of media (photo, video, or GIF) per message.1
Upload media
Use the Media Upload endpoint to upload your file and get a
media_id.2
Include in message
- The authenticated user must have uploaded the media
- Media is available for 24 hours after upload
- Only one attachment per message is supported
Sharing Posts
Include a Post in your message by adding the Post URL to the text:referenced_tweets field with the Post ID.
Message requirements
| Field | Required | Notes |
|---|---|---|
text | Yes* | Required if no attachments |
attachments | Yes* | Required if no text |
text or attachments must be provided.
ID compatibility with v1.1
Conversation and event IDs are shared between v1.1 and v2 endpoints. This enables hybrid workflows:- Create messages with v2
- Delete messages with v1.1 (not yet available in v2)
- Reference conversation IDs from x.com URLs
Error handling
| Status | Error | Solution |
|---|---|---|
| 400 | Invalid request | Check request body format |
| 401 | Unauthorized | Verify access token |
| 403 | Forbidden | Check scopes and user permissions |
| 429 | Too Many Requests | Wait and retry |
Common issues
Cannot send to user
Cannot send to user
The recipient may have DM settings that prevent messages from unknown users, or may have blocked you.
Media attachment failed
Media attachment failed
Ensure the media was uploaded by the same authenticated user and is less than 24 hours old.
Group creation failed
Group creation failed
Verify all participant IDs are valid and the users allow group DM invites.