X (Twitter)

twitter.com

Schedule, update and delete posts on X.

Using the X (Twitter) API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your X (Twitter) background job resumable and appear in our dashboard.

  • Use io.runTask() and the official SDK or fetch.

  • Example code using X (Twitter)

    Below is a working code example of how you can use X (Twitter) with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

    import { TriggerClient, eventTrigger } from "@trigger.dev/sdk";
    import z from "zod";
    import { createHmac } from "crypto";
    import OAuth from "oauth-1.0a";
    // Create tokens at
    // https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api
    const endpointURL = "https://api.twitter.com/2/tweets";
    const token = {
    key: process.env.X_ACCESS_TOKEN!,
    secret: process.env.X_ACCESS_TOKEN_SECRET!,
    };
    // Using OAuth 1.0a
    const oauth = new OAuth({
    consumer: {
    key: process.env.X_CONSUMER_KEY!,
    secret: process.env.X_CONSUMER_SECRET!,
    },
    signature_method: "HMAC-SHA1",
    hash_function: (baseString: string, key: string) =>
    createHmac("sha1", key).update(baseString).digest("base64"),
    });
    // Create an authorization header
    const authHeader = oauth.toHeader(
    oauth.authorize({ url: endpointURL, method: "POST" }, token)
    );
    // Create request options
    const requestOptions: RequestInit = {
    method: "POST",
    headers: {
    Authorization: authHeader["Authorization"],
    "user-agent": "v2CreateTweetJS",
    "content-type": "application/json",
    accept: "application/json",
    },
    };
    client.defineJob({
    id: "tweet-x",
    name: "Tweet X",
    version: "1.0.0",
    trigger: eventTrigger({
    name: "tweet-x",
    schema: z.object({
    text: z.string().max(280), // Tweets are limited to 280 characters
    }),
    }),
    run: async (payload, io, ctx) => {
    // Wrap an SDK call in io.runTask so it's resumable and displays in logs
    await io.runTask(
    "Tweet X",
    async () => {
    // Make request using Fetch API
    return await fetch(endpointURL, {
    ...requestOptions,
    body: JSON.stringify(payload),
    }).then((response) => response.json());
    },
    // Add metadata to improve how the task displays in the logs
    { name: "Tweet X", icon: "twitter" }
    );
    },
    });
    ,