Get AI summaries of any video or article — Sign up free
Step-by-Step Tutorial: How to Post to X-Twitter Using Agentic AI and n8n thumbnail

Step-by-Step Tutorial: How to Post to X-Twitter Using Agentic AI and n8n

Alex, PhD AI·
5 min read

Based on Alex, PhD AI's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Collect topic, optional reference link, and optional keywords/hashtags via a form to seed content generation.

Briefing

A step-by-step workflow ties together a web form, agentic text generation, email-based human approval, and automated posting to X (Twitter). The core idea is simple: collect a topic (plus optional keywords/hashtags and a reference link), generate platform-specific tweet copy in a structured format, send it to the user via email for approval or rejection, and—only after approval—publish the final post to X using the Twitter API.

The build starts with a form (“my form”) that gathers three inputs: a required “topic” field, an optional “link to relevant materials,” and optional “keywords or hashtags.” Submitting the form triggers the workflow and returns the collected values to downstream nodes. A test submission confirms the form is wired correctly before any automation logic is added.

Next comes the “social media content factory” agent, whose prompt is treated as the critical control layer. It’s instructed to produce concise, crisp, Twitter-optimized content (including SEO/hashtag strategy and engagement prompts) and to keep each tweet to 150 characters or less. The agent must output a specific JSON schema: for Twitter it returns the tweet text (“post”), an array of hashtags, plus suggestions for “video” and “image” (not automated yet). The workflow also includes a structured output parser to enforce that JSON shape, and an OpenAI chat model to generate the content.

A second agent then converts the generated JSON into a clean, modern HTML email. The email is table-based, includes a card per platform (with title, post, video/image suggestions, hashtag list, emoji, and a platform-specific call to action), and strips out code blocks and comments. The workflow uses a Gmail “send and wait” node to email the user an approval request, with a subject and message populated from the generated content.

The email response drives the decision point: the workflow waits for an approval action (approve vs disapprove/decline) with a 45-minute time limit. If approved, an “if” node routes the data to a Twitter “create tweet” action; if declined, nothing gets posted.

Publishing to X requires setting up a Twitter developer app (web app/automated app/bot), enabling read/write permissions and direct messages, and providing OAuth callback and website URL values. The workflow uses the Twitter node to post the approved tweet text to X.

A live test uses a topic based on OpenAI’s Codex release, along with keywords like “Codex,” “open AI,” “chat GPT,” “code generation,” and “agentic AI.” The email arrives with the proposed tweet and approve/decline buttons. After approval, the workflow posts an adapted tweet to X within seconds, including the suggested hashtags. The build ends by pointing to future expansions—threads and additional platforms like Instagram and Facebook—using the same agentic generation and approval pattern.

Cornell Notes

The workflow automates X posting by combining form inputs, agentic content generation, structured JSON output, and a human approval gate. A “social media content factory” agent uses a carefully designed prompt to generate Twitter-ready copy (≤150 characters) plus hashtags and media suggestions, returning results in a strict JSON schema. A second agent turns that JSON into a modern HTML email, which is sent via Gmail with approve/decline buttons. Only when the email response is approved does the workflow publish the tweet to X using a configured Twitter developer app and the Twitter API. This approval step prevents unwanted posts while still speeding up content creation.

How does the workflow ensure the tweet text and hashtags are usable for posting to X?

It enforces a structured output contract. The content-generation agent (“social media content factory”) is paired with a structured output parser that requires a JSON object containing platform-specific fields for Twitter: the tweet text (“post”), an array of hashtags, and separate “video suggestion” and “image suggestion” fields. The prompt also constrains the tweet length to 150 characters or less and instructs the agent to pull hashtags from the form inputs.

Why is the email approval step central to the pipeline’s reliability?

The workflow uses a Gmail “send and wait response” node and then branches on the result. The email asks for an approval action (approve vs disapprove/decline) and the workflow waits up to 45 minutes. If approved, it routes to the Twitter “create tweet” node; if declined, it stops without posting. This creates a human-in-the-loop control that reduces the risk of publishing incorrect or unwanted content.

What role does the second agent play after the JSON is generated?

It converts machine-readable JSON into a user-friendly HTML email. The prompt requires a table-based layout and a card section per platform, including the title, post text, video/image suggestions, hashtag list, emoji, and a call to action. It also instructs the model to output raw HTML only (no code blocks or comments) and to follow an example HTML structure.

How are Twitter API credentials and app settings used in the posting step?

Publishing requires a Twitter developer app configured with appropriate permissions (read and write, plus direct messages) and an application type such as web app/automated app/bot. The workflow uses OAuth callback and website URL values, then stores the client ID and client secret in the Twitter node. After connecting successfully, the Twitter node can create a tweet using the approved “platform post x Twitter” text from the agent output.

What inputs drive the content generation in the form?

The form collects a required “topic,” an optional “link to relevant materials,” and optional “keywords or hashtags.” Those values feed the content-generation agent, which can also analyze the provided reference link and adapt tone and style to Twitter guidelines. The agent then produces tweet copy and hashtags aligned with those inputs.

Review Questions

  1. What specific JSON fields does the structured output parser require for Twitter, and how do those fields map to what gets posted?
  2. How does the workflow decide whether to post to X after the email is sent, and what is the time limit for that decision?
  3. Which prompt constraints are used to keep tweets Twitter-appropriate (e.g., length and engagement style), and where do the hashtags come from?

Key Points

  1. 1

    Collect topic, optional reference link, and optional keywords/hashtags via a form to seed content generation.

  2. 2

    Use a “social media content factory” agent with a strict prompt and a structured output parser to produce Twitter-ready JSON (tweet text, hashtag array, media suggestions).

  3. 3

    Generate a clean, table-based HTML approval email from the JSON using a second agent, and send it through Gmail with a wait-for-response step.

  4. 4

    Gate posting with human approval: only an “approve” response triggers the Twitter create-tweet action; declines stop the workflow.

  5. 5

    Configure a Twitter developer app with read/write permissions and correct OAuth callback/website URL values, then connect credentials to the Twitter node.

  6. 6

    Enforce tweet length and engagement requirements in the generation prompt (e.g., ≤150 characters and a call to action).

  7. 7

    Test end-to-end with a real topic and reference link to confirm the email arrives correctly and the approved tweet posts to X.

Highlights

The workflow uses a structured JSON schema to force the content agent to output exactly what the Twitter posting step needs: tweet text plus a hashtag array.
A Gmail “send and wait response” node creates a human approval checkpoint; the pipeline won’t post to X unless the user approves within 45 minutes.
The HTML email is generated from JSON by a dedicated agent, producing a card-style, table-based layout with approve/decline controls.
Twitter posting is fully automated after approval by connecting a configured Twitter developer app and using the Twitter node’s create-tweet action.
Tweet quality is controlled through prompt constraints: crisp tone, platform-optimized engagement, and a hard 150-character limit.

Topics

Mentioned

  • Alex Gorbage