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

# How to Host X (Twitter) Bots

> Learn how to create and host X (Twitter) bots on Shard Cloud.

## Introduction

This guide covers creating and deploying X (Twitter) bots on Shard Cloud.

## Creating an X App

<Steps>
  <Step title="Access Developer Portal">
    Go to [X Developer Portal](https://developer.x.com/en/portal/dashboard) and
    log in.
  </Step>

  <Step title="Create Project">
    Click **"Create Project"**, choose a name, and select your use case.
  </Step>

  <Step title="Create App">Create an app within your project.</Step>

  <Step title="Generate API Keys">
    In **"Keys and tokens"**, generate: - API Key (Consumer Key) - API Secret
    Key (Consumer Secret) - Access Token - Access Token Secret
  </Step>

  <Step title="Set Permissions">
    Go to **"App permissions"** and select **"Read and write"**.
  </Step>
</Steps>

<Warning>
  **Security**: Store all credentials securely. Never commit them to version
  control.
</Warning>

## Creating Your Bot

### Setup

```bash theme={null}
npm init -y
npm install twitter-api-v2
```

### Basic Bot Code

```javascript index.js theme={null}
const { TwitterApi } = require("twitter-api-v2");

const client = new TwitterApi({
  appKey: process.env.API_KEY,
  appSecret: process.env.API_SECRET_KEY,
  accessToken: process.env.ACCESS_TOKEN,
  accessSecret: process.env.ACCESS_TOKEN_SECRET,
});

const rwClient = client.readWrite;

async function verifyBot() {
  try {
    const user = await rwClient.currentUser();
    console.log(`Bot initialized! User: @${user.screen_name}`);
    return true;
  } catch (error) {
    console.error("Error verifying bot:", error);
    return false;
  }
}

async function postTweet(text) {
  try {
    const tweet = await rwClient.v2.tweet(text);
    console.log(`Tweet posted! ID: ${tweet.data.id}`);
    return tweet;
  } catch (error) {
    console.error("Error posting tweet:", error);
    throw error;
  }
}

async function runBot() {
  console.log("Starting X bot...");

  const botOk = await verifyBot();
  if (!botOk) {
    console.error("Bot initialization failed");
    return;
  }

  // Keep the bot running
  setInterval(() => {
    console.log("Bot is alive...");
  }, 60000);
}

runBot();
```

### Package.json

```json package.json theme={null}
{
  "name": "x-twitter-bot",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "twitter-api-v2": "^1.15.0"
  }
}
```

## Environment Variables

Set these in the Shard Cloud dashboard:

* `API_KEY`: Your X API key
* `API_SECRET_KEY`: Your X API secret
* `ACCESS_TOKEN`: Your access token
* `ACCESS_TOKEN_SECRET`: Your access token secret

## Shard Cloud Configuration

```systemd .shardcloud theme={null}
DISPLAY_NAME=X Twitter Bot
DESCRIPTION=X/Twitter Automation Bot
MAIN=index.js
MEMORY=256
VERSION=recommended
```

## Deploying

<Steps>
  <Step title="Prepare Your Files">
    Ensure you have: - `index.js` - `package.json` - `.shardcloud`
  </Step>

  <Step title="Set Environment Variables">
    Configure all API credentials in the dashboard.
  </Step>

  <Step title="Create ZIP Archive">
    Compress your project folder (excluding `node_modules/`).
  </Step>

  <Step title="Upload to Shard Cloud">
    Go to [Shard Cloud Dashboard](https://shardcloud.app/dash/applications) and
    upload.
  </Step>
</Steps>

## Additional Resources

* [X API Documentation](https://docs.x.com/overview)
* [twitter-api-v2 Library](https://github.com/PLhery/node-twitter-api-v2)

## Troubleshooting

<AccordionGroup>
  <Accordion title="Authentication errors">
    * Verify all four credentials are correct - Check that permissions are set
      to "Read and write" - Regenerate tokens if needed
  </Accordion>

  <Accordion title="Rate limit exceeded">
    Implement rate limiting and reduce request frequency.
  </Accordion>
</AccordionGroup>
