Crosspost
A JavaScript utility for posting across multiple social networks at once
Install / Use
/learn @humanwhocodes/CrosspostREADME
Crosspost
If you find this useful, please consider supporting my work with a donation.
Description
A utility for posting across multiple social networks at once.
Installation
npm install @humanwhocodes/crosspost
Usage
API Usage
The API is split into two parts:
- The
Clientclass that can be used to post the same message across multiple services. - A number of different strategy implementations, one for each service:
BlueskyStrategyMastodonStrategyTwitterStrategyLinkedInStrategyDiscordStrategyDiscordWebhookStrategyTelegramStrategyDevtoStrategyNostrStrategy(requires Node.js v22+)
Each strategy requires its own parameters that are specific to the service. If you only want to post to a particular service, you can just directly use the strategy for that service.
import {
Client,
TwitterStrategy,
MastodonStrategy,
BlueskyStrategy,
LinkedInStrategy,
DiscordStrategy,
DiscordWebhookStrategy,
TelegramStrategy,
DevtoStrategy,
NostrStrategy,
} from "@humanwhocodes/crosspost";
// Note: Use an app password, not your login password!
const bluesky = new BlueskyStrategy({
identifier: "me.you.social",
password: "your-app-password",
host: "you.social", // "bsky.social" for most people
});
// Note: Personal access token is required
const mastodon = new MastodonStrategy({
accessToken: "your-access-token",
host: "mastodon.host",
});
// Note: OAuth app is required
const twitter = new TwitterStrategy({
accessTokenKey: "access-token-key",
accessTokenSecret: "access-token-secret",
apiConsumerKey: "api-consumer-key",
apiConsumerSecret: "api-consumer-secret",
});
// Note: OAuth access token is required
const linkedin = new LinkedInStrategy({
accessToken: "your-access-token",
});
// Note: Bot token and channel ID required
const discord = new DiscordStrategy({
botToken: "your-bot-token",
channelId: "your-channel-id",
});
// Note: Webhook URL required
const discordWebhook = new DiscordWebhookStrategy({
webhookUrl: "your-webhook-url",
});
// Note: Bot token and chat ID required
const telegram = new TelegramStrategy({
botToken: "your-bot-token",
chatId: "your-chat-id",
});
// Note: API key required
const devto = new DevtoStrategy({
apiKey: "your-api-key",
});
// Note: Private key and relays required
const nostr = new NostrStrategy({
privateKey: "your-private-key", // hex or bech32 format
relays: ["wss://relay.example.com", "wss://relay2.example.com"],
});
// create a client that will post to all services
const client = new Client({
strategies: [
bluesky,
mastodon,
twitter,
linkedin,
discord,
discordWebhook,
telegram,
devto,
nostr,
],
});
// post to all services with up to 4 images (must be PNG, JPEG, or GIF)
await client.post("Hello world!", {
images: [
{
data: imageData, // Uint8Array of image data
alt: "Description of the image",
},
],
});
// post to all services with an abort signal
const controller = new AbortController();
await client.post("Hello world!", { signal: controller.signal });
// post to specific services using postTo
await client.postTo([
{
message: "Hello Twitter!",
strategyId: "twitter", // Uses the strategy's id property
},
{
message: "Hello Mastodon and Bluesky!",
strategyId: "mastodon",
images: [
{
data: imageData, // Uint8Array of image data
alt: "Description of the image",
},
],
},
{
message: "Hello Bluesky!",
strategyId: "bluesky",
},
]);
// post to specific services with a signal
await client.postTo(
[
{
message: "Hello Twitter!",
strategyId: "twitter",
},
{
message: "Hello Mastodon!",
strategyId: "mastodon",
},
],
{ signal: controller.signal },
);
CLI Usage
Crosspost also has a command line interface to allow for incorporation into CI systems.
Usage: crosspost [options] ["Message to post."]
--twitter, -t Post to Twitter.
--mastodon, -m Post to Mastodon.
--bluesky, -b Post to Bluesky.
--linkedin, -l Post to LinkedIn.
--discord, -d Post to Discord via bot.
--discord-webhook Post to Discord via webhook.
--devto Post to dev.to.
--telegram Post to Telegram.
--slack, -s Post to Slack.
--nostr, -n Post to Nostr.
--mcp Start MCP server.
--file The file to read the message from.
--image The image file to upload with the message.
--image-alt Alt text for the image (default: filename).
--help, -h Show this message.
--version, -v Show version number.
Examples:
# Post a message to multiple services
npx @humanwhocodes/crosspost -t -m -b "Check out this beach!"
# Post a message with an image to multiple services
npx @humanwhocodes/crosspost -t -m -b --image ./photo.jpg --image-alt "A beautiful sunset" "Check out this beach!"
This posts the message "Hello world!" to Twitter, Mastodon, and Bluesky with an attached image. You can choose to post to any combination by specifying the appropriate command line options.
You can also read the message from a file instead of from the command line:
# Post a message to multiple services
npx @humanwhocodes/crosspost -t -m -b -f message.txt
# Post a message with an image to multiple services
npx @humanwhocodes/crosspost -t -m -b -f message.txt -i path/to/image.jpg
Each strategy requires a set of environment variables in order to execute:
- Twitter
TWITTER_ACCESS_TOKEN_KEYTWITTER_ACCESS_TOKEN_SECRETTWITTER_API_CONSUMER_KEYTWITTER_API_CONSUMER_SECRET
- Mastodon
MASTODON_ACCESS_TOKENMASTODON_HOST
- Bluesky
BLUESKY_HOSTBLUESKY_IDENTIFIERBLUESKY_PASSWORD
- LinkedIn
LINKEDIN_ACCESS_TOKEN
- Discord
DISCORD_BOT_TOKENDISCORD_CHANNEL_ID
- Discord Webhook
DISCORD_WEBHOOK_URL
- dev.to
DEVTO_API_KEY
- Telegram
TELEGRAM_BOT_TOKENTELEGRAM_CHAT_ID
- Slack
SLACK_TOKENSLACK_CHANNEL
- Nostr
NOSTR_PRIVATE_KEYNOSTR_RELAYS
Tip: You can load environment variables from a .env file by setting the environment variable CROSSPOST_DOTENV. Set it to 1 to use .env in the current working directory, or set it to a specific filepath to use a different location.
MCP Server Usage
Crosspost can be run as an MCP (Model Context Protocol) server, which allows it to be used by AI agents:
npx @humanwhocodes/crosspost --mcp -t -m -b
This starts an MCP server that can post to Twitter, Mastodon, and Bluesky. The server provides prompts and tools for posting to all services or individual services. Only the services indicated by the flags are available via the server.
To run the MCP server through the MCP Inspector for debugging purposes, run the following command:
npx run mcp:inspect -- -t -m -b
Using the MCP Server with Claude Desktop
To use the MCP server with Claude you must have Node.js installed. Then, in Claude Desktop:
- Click on File -> Settings.
- Select "Developer".
- Click "Edit Config".
Claude will then create a claude_desktop_config.json file. Open it and add the following:
{
"mcpServers": {
"crosspost": {
"command": "npx",
"args": ["@humanwhocodes/crosspost", "-m", "-l", "--mcp"],
"env": {
"LINKEDIN_ACCESS_TOKEN": "abcdefghijklmnop",
"MASTODON_ACCESS_TOKEN": "abcdefghijklmnop",
"MASTODON_HOST": "mastodon.social"
}
}
}
}
This example enables Mastodon and LinkedIn so the env key contains the environment variables necessary to post to those services. You can customize the services by passing different command line arguments as you would using the CLI.
If you'd prefer not to put your environment variables directly into the JSON file, you can create a .env file and use the CROSSPOST_DOTENV environment variable to point to it:
{
"mcpServers": {
"crosspost": {
"command": "npx",
"args": ["@humanwhocodes/crosspost", "-m", "-l", "-t", "--mcp"],
"env": {
"CROSSPOST_DOTENV": "/usr/nzakas/settings/.env"
}
}
}
}
Here are some prompts you can try:
- "Crosspost this message: Hello world!" (posts to all available services)
- "Post this to Twitter: Hello X!" (posts just to Twitter)
- "Post this to Mastodon and Bluesky: Hello friends!" (posts to Mastodon and Bluesky)
Setting up Strategies
Each strategy uses the service's preferred way of posting messages, so you'll need to follow specific steps in order to enable API access.
To enable posting on Twitter, you'll need to create a free developer account and an OAuth application. Follow these instructions.
Generally speaking, if you are creating an app to automate your own posts, you'll be able to use it for free so long as you're not posting a large number of times per day.
Note: The post uses the terms "app key" and "app secret" whereas the Twitter strategy here uses "API consumer key" and "API consumer secret". They are the same values.
Mastodon
To enable posting to Mastodon, you'll need to create a new application:
- Log in to your Mastodon server.
- Click on "Edit Profile".
- Click on "Development".
- Click "New Application".
- Give your application a name.
- Check off
write:statusesfor your scope. If you want to upload images, check offwrite:mediatoo. - Click "Submit".
This will generate a client key, client secret, and access token. You only need to use the access token when posting via the API.
Bluesky
Bluesky doesn't require an application for automated posts, only your identifier and an app password. To generate an app password:
- Log in to your Bluesky account.
- Click "Settings".
- Click "Privacy and Security."
- Cl
