@humanwhocodes/crosspost

repository·main·Indexed 19 days ago

https://github.com/humanwhocodes/crosspost

A utility for posting messages and images across multiple social media platforms simultaneously via a unified API or CLI. Supported services include Twitter, Mastodon, Bluesky, LinkedIn, Discord (Bot and Webhook), Telegram, Dev.to, Slack, and Nostr. It features a Client API for orchestration and can be run as a Model Context Protocol (MCP) server to enable AI agents to post to social networks.

Tokens
8.9K
Snippets
28
Records
43
Agent score
69%

What's inside @humanwhocodes/crosspost

  1. How the Crosspost API works

    main

    The Crosspost API is built around two main components:

    1. Strategies: Individual implementations for specific services (e.g., TwitterStrategy, MastodonStrategy). Each strategy requires service-specific configuration parameters (like API keys or access tokens).
    2. Client: A central Client class that orchestrates posting. You instantiate a Client by passing an array of configured strategy instances to its strategies option. Once configured, the client can broadcast a single message to all registered services or target specific ones.

    Supported strategies include:

    • BlueskyStrategy
    • MastodonStrategy
    • TwitterStrategy
    • LinkedInStrategy
    • DiscordStrategy
    • DiscordWebhookStrategy
    • TelegramStrategy
    • DevtoStrategy
    • NostrStrategy (requires Node.js v22+)
    import {
    	Client,
    	TwitterStrategy,
    	MastodonStrategy,
    	// ... other strategies
    } from "@humanwhocodes/crosspost";
    
    const mastodon = new MastodonStrategy({
    	accessToken: "your-access-token",
    	host: "mastodon.host",
    });
    
    const client = new Client({
    	strategies: [mastodon],
    });
    
    await client.post("Hello world!");
  2. Configure Dev.to strategy

    main

    To enable posting to Dev.to:

    1. Log in to Dev.to.
    2. Go to Settings > Extensions.
    3. Scroll to DEV Community API Keys and click Generate API Key.
    4. Copy the key.

    When using the CLI, provide this key via the DEVTO_API_KEY environment variable. The first line of your post will be used as the article title.

  3. Configure Discord Bot strategy

    main

    To post to Discord using a bot, follow these steps in the Discord Developer Portal:

    1. Create Application: Create a new application and name it.
    2. Installation: Under Installation, set Install Link to None and save.
    3. Bot Setup:
      • Under Bot, uncheck Public Bot.
      • Under Text Permissions, check Send Messages.
      • Click Reset Token and copy the bot token.
    4. Add Bot to Server:
      • Go to OAuth2 > OAuth2 URL Generator.
      • Check bot and under Bot Permissions, check Send Messages.
      • Copy the URL, open it in a browser, and authorize it for your server.
    5. Get Channel ID: Enable Developer Mode in Discord (User Settings > Advanced), then right-click the target channel and select Copy Channel ID.

    Note: By default, bots can only post to public channels unless granted specific permissions for private channels.

  4. Use Crosspost as an MCP Server

    main

    Crosspost can run as a Model Context Protocol (MCP) server, enabling AI agents (like Claude Desktop) to post to social networks.

    Start the server:

    npx @humanwhocodes/crosspost --mcp -t -m -b

    (The flags -t -m -b determine which services are available to the agent).

    Claude Desktop Configuration: Add the server to your claude_desktop_config.json file. You can pass environment variables directly in the env block or use CROSSPOST_DOTENV to point to a .env file.

    Example Configuration (Direct Env):

    {
    	"mcpServers": {
    		"crosspost": {
    			"command": "npx",
    			"args": ["@humanwhocodes/crosspost", "-m", "-l", "--mcp"],
    			"env": {
    				"LINKEDIN_ACCESS_TOKEN": "abcdefghijklmnop",
    				"MASTODON_ACCESS_TOKEN": "abcdefghijklmnop",
    				"MASTODON_HOST": "mastodon.social"
    			}
    		}
    	}
    }
    {
    	"mcpServers": {
    		"crosspost": {
    			"command": "npx",
    			"args": ["@humanwhocodes/crosspost", "-m", "-l", "--mcp"],
    			"env": {
    				"CROSSPOST_DOTENV": "/path/to/.env"
    			}
    		}
    	}
    }
  5. Configure Bluesky strategy

    main

    Bluesky does not require a formal application. You only need your identifier and an app password.

    To generate an app password:

    1. Log in to Bluesky.
    2. Go to Settings > Privacy and Security > App Passwords.
    3. Click Add App Password, name it, and copy the generated password.

    Important: Do not use your primary login password with the API.

  6. Configure LinkedIn strategy

    main

    To enable posting to LinkedIn, you must create an app via LinkedIn Developers and follow these steps:

    1. Create an app and go to the Settings tab.
    2. Verify your LinkedIn Page.
    3. Under Available Products, request access to:
      • Share on LinkedIn
      • Sign in with LinkedIn using OpenID Connect
    4. Use OAuth 2.0 Tools to Create Token.
    5. Select your app and check the following scopes:
      • openid
      • profile
      • w_member_social
    6. Click Request Access Token and authorize via your profile.

    Important: Tokens automatically expire after two months.

  7. Configure Nostr strategy

    main

    To post to Nostr relays, you need a private key and a list of relays.

    1. Private Key (NOSTR_PRIVATE_KEY):

    • Use a hex format (64 characters) or bech32 format (starting with nsec1).
    • Security: Use a dedicated key for crossposting rather than your main identity key.

    2. Relays (NOSTR_RELAYS):

    • Provide multiple WebSocket URLs separated by commas (e.g., "wss://relay.damus.io","wss://nos.lol").

    Limitations:

    • Nostr posts are "short text notes" (kind 1 events) with a 280 character limit.
    • Images are not supported in Nostr text notes.
    • Requirement: Nostr support requires Node.js v22+.
  8. Configure Discord Webhook strategy

    main

    To post to Discord via a webhook:

    1. In Discord, go to your server settings for the target channel.
    2. Navigate to Integrations > Webhooks.
    3. Click New Webhook, name it, and select the channel.
    4. Click Copy Webhook URL.

    Use the copied URL as the webhookUrl parameter in the DiscordWebhookStrategy configuration.

  9. Configure Telegram strategy

    main

    To post to Telegram using a bot, you need a bot token and a chat ID.

    1. Get Bot Token:

    • Message @BotFather on Telegram and send /newbot.
    • Follow prompts to name your bot and create a username.
    • Copy the provided token and set it as the TELEGRAM_BOT_TOKEN environment variable.

    2. Get Chat ID:

    • You can use a username (e.g., @username).
    • To get a specific ID, message @userinfobot.
    • For group IDs, add your bot to the group and use @RawDataBot to find the ID.
    • Set the value as the TELEGRAM_CHAT_ID environment variable.
  10. Configure Slack strategy

    main

    To post to Slack using a bot, follow these steps in the Slack API website:

    1. Create App: Select From scratch, name it, and select your workspace.
    2. Permissions: Under OAuth & Permissions > Scopes > Bot Token Scopes, add:
      • chat:write (to send messages)
      • files:write (required for image support)
    3. Install: Click Install to Workspace and allow permissions.
    4. Token: Copy the Bot User OAuth Token (starts with xoxb-) and set it as the SLACK_TOKEN environment variable.
    5. Channel: Right-click the channel in Slack and select Copy link. The channel ID is the part after the last slash (e.g., C1234567890). Set this as SLACK_CHANNEL (or use the channel name like #general).

    Note: The bot must be added to the channel (e.g., by mentioning it or using /invite @your-bot-name). The bot can only message users who have previously messaged it or added it to a group.