> ## Documentation Index
> Fetch the complete documentation index at: https://docs.postqueen.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# X (Twitter) setup

> Register the X (Twitter) application and give PostQueen its keys

On your own instance the X app is yours: create it in the X developer portal, then put its API key and secret in your environment.

X is a bit different.

They created an oAuth2 flow, but it works only with Twitter v2 API.
But in order to upload pictures to X, you need to use the old Twitter v1 API.
So you are going to use the normal oAuth1 flow for that (that supports Twitter v2 also 🤷🏻‍).

<Steps>
  <Step title="Create a new app">
    Head over the [Twitter developers page](https://developer.twitter.com/en/portal/dashboard) and create a new app.
    Click to sign-up for a new free account

    <Frame>
      <img src="https://mintcdn.com/forceplay/Gsgj4WOu3HFxkte6/images/providers/x/x-001.png?fit=max&auto=format&n=Gsgj4WOu3HFxkte6&q=85&s=89f7e7a05eddfce90ee207401aff0ed1" alt="The X developer portal dashboard, with the sign-up link for a new free account" width="1238" height="368" data-path="images/providers/x/x-001.png" />
    </Frame>
  </Step>

  <Step title="Edit application settings">
    Click to edit the application settings

    <Frame>
      <img src="https://mintcdn.com/forceplay/Gsgj4WOu3HFxkte6/images/providers/x/x-002.png?fit=max&auto=format&n=Gsgj4WOu3HFxkte6&q=85&s=d03836329d468bc43ddb62567cdb131c" alt="The app overview in the X developer portal, with the settings edit control highlighted" width="1660" height="852" data-path="images/providers/x/x-002.png" />
    </Frame>
  </Step>

  <Step title="Set up authentication flow">
    Click to set up an authentication flow

    <Frame>
      <img src="https://mintcdn.com/forceplay/Gsgj4WOu3HFxkte6/images/providers/x/x-003.png?fit=max&auto=format&n=Gsgj4WOu3HFxkte6&q=85&s=df109e5f82e91335d95b5846c110e45f" alt="The user authentication settings screen, where App Permission, Type of App and the callback URL are set" width="1668" height="420" data-path="images/providers/x/x-003.png" />
    </Frame>

    * In the App Permission set it to `Read and Write`
    * In the Type of App set it to `Native App`
    * In the App Info set the `Callback URI / Redirect URL`

    <Warning>
      You must select `Native App` for OAuth 1.0a to work correctly. Selecting `Web App, Automated App or Bot` will cause authentication to fail with error code 32.
    </Warning>
  </Step>

  <Step title="Configure OAuth2 Redirect URI">
    <Snippet file="oauth2redirect.mdx" />

    **Your X OAuth2 Redirect URI:**

    * Production: `https://postqueen.example.com/integrations/social/x`
    * Local development: `http://localhost:4200/integrations/social/x`
    * Docker: `http://localhost:4007/integrations/social/x`
    * If X requires HTTPS for localhost: `https://redirectmeto.com/http://localhost:4200/integrations/social/x`
  </Step>

  <Step title="Copy your API keys">
    Save it and go to "Keys and Tokens" tab.

    Click on "Regenerate" inside "Consumer Keys" and copy the `API Key` and `API Key Secret`.

    Open .env file and add the following:

    ```env theme={"system"}
    X_API_KEY=""
    X_API_SECRET=""
    ```
  </Step>

  <Step title="Restart and add the X channel">
    <Snippet file="restart-and-add-channel.mdx" />
  </Step>
</Steps>

## Optional environment variables

* `X_URL`: override the base used to build the X OAuth **callback**, which becomes `<X_URL>/integrations/social/x`. Defaults to `FRONTEND_URL`. It does not change which X API PostQueen talks to, and setting it to anything your registered callback does not match will break the connect.
* `DISABLE_X_ANALYTICS=true`: skip the analytics fetch on the X channel page. Useful if your X plan does not include analytics access and the failed call is noisy.
* `STRIP_LINKS_FROM_X_POSTS=true`: automatically remove URLs from post text before publishing. Some accounts see better reach with no links; this enforces it globally.

## Next steps

<CardGroup cols={2}>
  <Card title="What she can post to X" icon="https://mintcdn.com/forceplay/LL5FipRv-7nhg1To/images/channels/x.svg?fit=max&auto=format&n=LL5FipRv-7nhg1To&q=85&s=33f1473cab5ef90ac0e4e3e14a30b088" href="/providers/x-twitter" width="64" height="64" data-path="images/channels/x.svg">
    Limits, post types and settings
  </Card>

  <Card title="Configuration reference" icon="list" href="/configuration/reference">
    Every variable she reads
  </Card>

  <Card title="Connect errors" icon="plug-circle-xmark" href="/troubleshooting/oauth-connect">
    Invalid state, invalid\_grant and the rest
  </Card>

  <Card title="Another channel" icon="plug" href="/providers/overview">
    Every network, and what each one asks for
  </Card>
</CardGroup>
