> ## Documentation Index
> Fetch the complete documentation index at: https://x-preview-mintlify-04d20e4e.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Quickstart

> This guide walks you through sending Direct Messages and creating group conversations. Reference for the X API v2 standard tier covering manage.

export const Button = ({href, children}) => {
  return <div className="not-prose group">
    <a href={href}>
      <button className="flex items-center space-x-2.5 py-1 px-4 bg-primary-dark dark:bg-white text-white dark:text-gray-950 rounded-full group-hover:opacity-[0.9] font-medium">
        <span>
          {children}
        </span>
        <svg width="3" height="24" viewBox="0 -9 3 24" class="h-6 rotate-0 overflow-visible"><path d="M0 0L3 3L0 6" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"></path></svg>
      </button>
    </a>
  </div>;
};

This guide walks you through sending Direct Messages and creating group conversations.

<Note>
  **Prerequisites**

  Before you begin, you'll need:

  * A [developer account](https://developer.x.com/en/portal/petition/essential/basic-info) with an approved App
  * User Access Token (OAuth 2.0 PKCE with `dm.write` and `dm.read` scopes)
</Note>

***

## Send a one-to-one message

<Steps>
  <Step title="Get the recipient's user ID">
    You need the user ID of the person you want to message. You can get this from the [User lookup endpoint](/x-api/users/lookup/introduction).
  </Step>

  <Step title="Send the message">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/dm_conversations/with/9876543210/messages" \
        -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{"text": "Hello! This is a message from the API."}'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

      # Send a one-to-one message
      response = client.dm.send_message(
          participant_id="9876543210",
          text="Hello! This is a message from the API."
      )

      print(f"Message sent: {response.data.dm_event_id}")
      print(f"Conversation: {response.data.dm_conversation_id}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

      // Send a one-to-one message
      const response = await client.dm.sendMessage({
        participantId: "9876543210",
        text: "Hello! This is a message from the API.",
      });

      console.log(`Message sent: ${response.data?.dm_event_id}`);
      console.log(`Conversation: ${response.data?.dm_conversation_id}`);
      ```
    </CodeGroup>
  </Step>

  <Step title="Review the response">
    ```json theme={null}
    {
      "data": {
        "dm_conversation_id": "1234567890-9876543210",
        "dm_event_id": "1582103724607971332"
      }
    }
    ```
  </Step>
</Steps>

***

## Create a group conversation

<Steps>
  <Step title="Define participants">
    Gather the user IDs of everyone you want in the group (excluding yourself).
  </Step>

  <Step title="Create the group with first message">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/dm_conversations" \
        -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "conversation_type": "Group",
          "participant_ids": ["944480690", "906948460078698496"],
          "message": {"text": "Welcome to our new group!"}
        }'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

      # Create a group conversation
      response = client.dm.create_conversation(
          conversation_type="Group",
          participant_ids=["944480690", "906948460078698496"],
          message={"text": "Welcome to our new group!"}
      )

      print(f"Group created: {response.data.dm_conversation_id}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

      // Create a group conversation
      const response = await client.dm.createConversation({
        conversationType: "Group",
        participantIds: ["944480690", "906948460078698496"],
        message: { text: "Welcome to our new group!" },
      });

      console.log(`Group created: ${response.data?.dm_conversation_id}`);
      ```
    </CodeGroup>
  </Step>

  <Step title="Receive the conversation ID">
    ```json theme={null}
    {
      "data": {
        "dm_conversation_id": "1582103724607971328",
        "dm_event_id": "1582103724607971332"
      }
    }
    ```

    Save the `dm_conversation_id` to add more messages later.
  </Step>
</Steps>

***

## Add a message to an existing conversation

Send a message to a conversation you're already part of:

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl -X POST "https://api.x.com/2/dm_conversations/1582103724607971328/messages" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"text": "Adding another message to the conversation."}'
  ```

  ```python Python SDK theme={null}
  from xdk import Client

  client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

  # Add message to existing conversation
  response = client.dm.send_message_to_conversation(
      dm_conversation_id="1582103724607971328",
      text="Adding another message to the conversation."
  )

  print(f"Message sent: {response.data.dm_event_id}")
  ```

  ```javascript JavaScript SDK theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

  // Add message to existing conversation
  const response = await client.dm.sendMessageToConversation(
    "1582103724607971328",
    { text: "Adding another message to the conversation." }
  );

  console.log(`Message sent: ${response.data?.dm_event_id}`);
  ```
</CodeGroup>

***

## Send a message with media

<Steps>
  <Step title="Upload the media">
    First, upload your media using the [Media Upload endpoint](/x-api/media/quickstart/media-upload-chunked).
  </Step>

  <Step title="Send with media attachment">
    <CodeGroup dropdown>
      ```bash cURL theme={null}
      curl -X POST "https://api.x.com/2/dm_conversations/with/9876543210/messages" \
        -H "Authorization: Bearer $USER_ACCESS_TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
          "text": "Check out this image!",
          "attachments": [{"media_id": "1234567890123456789"}]
        }'
      ```

      ```python Python SDK theme={null}
      from xdk import Client

      client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

      # Send message with media
      response = client.dm.send_message(
          participant_id="9876543210",
          text="Check out this image!",
          attachments=[{"media_id": "1234567890123456789"}]
      )

      print(f"Message with media sent: {response.data.dm_event_id}")
      ```

      ```javascript JavaScript SDK theme={null}
      import { Client } from "@xdevplatform/xdk";

      const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

      // Send message with media
      const response = await client.dm.sendMessage({
        participantId: "9876543210",
        text: "Check out this image!",
        attachments: [{ mediaId: "1234567890123456789" }],
      });

      console.log(`Message with media sent: ${response.data?.dm_event_id}`);
      ```
    </CodeGroup>
  </Step>
</Steps>

***

## Delete a message

Delete a message you sent:

<CodeGroup dropdown>
  ```bash cURL theme={null}
  curl -X DELETE "https://api.x.com/2/dm_events/1582103724607971332" \
    -H "Authorization: Bearer $USER_ACCESS_TOKEN"
  ```

  ```python Python SDK theme={null}
  from xdk import Client

  client = Client(bearer_token="YOUR_USER_ACCESS_TOKEN")

  # Delete a message
  response = client.dm.delete_message("1582103724607971332")
  print(f"Deleted: {response.data.deleted}")
  ```

  ```javascript JavaScript SDK theme={null}
  import { Client } from "@xdevplatform/xdk";

  const client = new Client({ accessToken: "YOUR_USER_ACCESS_TOKEN" });

  // Delete a message
  const response = await client.dm.deleteMessage("1582103724607971332");
  console.log(`Deleted: ${response.data?.deleted}`);
  ```
</CodeGroup>

**Response:**

```json theme={null}
{
  "data": {
    "deleted": true
  }
}
```

<Warning>
  You can only delete messages you sent, not messages from other participants.
</Warning>

***

## Required scopes

When using OAuth 2.0 PKCE, your access token must have these scopes:

| Scope        | Description                                 |
| :----------- | :------------------------------------------ |
| `dm.write`   | Send and delete messages                    |
| `dm.read`    | Read conversations (required with dm.write) |
| `tweet.read` | Required for some expansions                |
| `users.read` | Required for user expansions                |

***

## Next steps

<CardGroup cols={2}>
  <Card title="DM lookup" icon="inbox" href="/x-api/direct-messages/lookup/quickstart">
    Retrieve DM conversations
  </Card>

  <Card title="Integration guide" icon="book" href="/x-api/direct-messages/manage/integrate">
    Key concepts and best practices
  </Card>

  <Card title="API Reference" icon="code" href="/x-api/direct-messages/create-dm-message-by-participant-id">
    Full endpoint documentation
  </Card>

  <Card title="Sample code" icon="github" href="https://github.com/xdevplatform/Twitter-API-v2-sample-code">
    Working code examples
  </Card>
</CardGroup>
