Skip to main content

Introduction

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

Creating an X App

1

Access Developer Portal

Go to X Developer Portal and log in.
2

Create Project

Click “Create Project”, choose a name, and select your use case.
3

Create App

Create an app within your project.
4

Generate API Keys

In “Keys and tokens”, generate: - API Key (Consumer Key) - API Secret Key (Consumer Secret) - Access Token - Access Token Secret
5

Set Permissions

Go to “App permissions” and select “Read and write”.
Security: Store all credentials securely. Never commit them to version control.

Creating Your Bot

Setup

npm init -y
npm install twitter-api-v2

Basic Bot Code

index.js
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

package.json
{
  "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

.shardcloud
DISPLAY_NAME=X Twitter Bot
DESCRIPTION=X/Twitter Automation Bot
MAIN=index.js
MEMORY=256
VERSION=recommended

Deploying

1

Prepare Your Files

Ensure you have: - index.js - package.json - .shardcloud
2

Set Environment Variables

Configure all API credentials in the dashboard.
3

Create ZIP Archive

Compress your project folder (excluding node_modules/).
4

Upload to Shard Cloud

Go to Shard Cloud Dashboard and upload.

Additional Resources

Troubleshooting

  • Verify all four credentials are correct - Check that permissions are set to “Read and write” - Regenerate tokens if needed
Implement rate limiting and reduce request frequency.