# How to Automate X (Twitter) Replies using the API

- Tool: API Automations
- Difficulty: Intermediate
- Fix time: 3–5 hours
- Compatibility: X API v2, Basic tier ($200/month) or pay-per-use
- Last updated: May 2026

## TL;DR

Automated replies on X use POST /2/tweets with a reply.in_reply_to_tweet_id field. Monitor mentions or keyword matches via GET /2/tweets/search/recent (7-day window, Basic tier required), then reply to relevant tweets. Key risk: error 226 (spam detection) fires aggressively on rapid or repetitive replies — vary text and add human-like delays. Cost on pay-per-use: $0.015 per text reply.

## Best practices

- Use -is:retweet -is:reply in your search query to only reply to original tweets mentioning you, not retweet chains
- Keep at least 8–10 reply templates per category and select randomly to avoid error 226
- Add a random delay of 8–30 seconds between replies — uniform delays look like automation
- Never include URLs in replies unless necessary — link replies cost $0.20 each vs $0.015 for text
- Poll every 5 minutes rather than every minute — Basic tier gives 180 search requests per 15 min, daily polling at 1-min intervals would exhaust this
- Monitor x-rate-limit-remaining on every response and preemptively pause when below 20% of limit
- Log every reply with the original tweet ID, reply ID, category, and timestamp for audit and debugging

## Frequently asked questions

### Do I need Basic tier ($200/month) to automate replies?

Yes, for practical use. The Free tier is effectively closed to new developers as of February 2026 and only allows 1 request per 24 hours on most read endpoints. Basic at $200/month gives you 180 search requests per 15 minutes and 50,000 writes/month. Alternatively, pay-per-use charges $0.015 per text reply and $0.005 per search read — calculate which is cheaper based on your expected volume.

### What triggers error 226 (spam detection) and how do I avoid it?

Error 226 has no publicly documented threshold, but it fires on three main patterns: replying too fast (under 8–10 seconds between replies), sending identical or near-identical text to multiple users, and replying to a large number of tweets in a short window. Mitigation: randomize delays 8–30 seconds, maintain at least 8 different reply templates per category, and pause for 30 minutes when 226 occurs.

### Can I use app-only Bearer token to post replies?

No. POST /2/tweets requires user-context OAuth 2.0 (Authorization Code with PKCE). App-only Bearer token is read-only and can fetch mentions and search results, but cannot post. You need the user's personal access token obtained via the PKCE flow.

### My access token expires in 2 hours — how do I handle this in a long-running bot?

Store both the access token and refresh token. Before each API call, check if the access token's expiry time (current time + 7200 seconds from when it was issued) is within 5 minutes of expiring. If so, call POST /2/oauth2/token with grant_type=refresh_token to get a fresh access token. The refresh token itself does not expire but should be rotated every 90 days.

### Can I reply to tweets mentioning keywords even if I'm not tagged?

Yes. Use GET /2/tweets/search/recent with a keyword query like 'your_keyword -is:retweet' instead of '@youraccount'. You must be on Basic tier — this endpoint is restricted on Free. Be aware that responding to keyword matches (not direct mentions) increases spam detection risk, as you're engaging with users who didn't explicitly address you.

### How can RapidDev help with building my X reply automation?

RapidDev can help you build the underlying infrastructure for your X reply bot — database schemas, token refresh workers, cost tracking dashboards, and Supabase integration for logging replied tweets. If you're building with Lovable or V0, we have step-by-step guides for connecting your bot backend to a visual dashboard.

---

Source: https://www.rapidevelopers.com/api-automations/how-to-automate-twitter-replies-using-the-api
© RapidDev — https://www.rapidevelopers.com/api-automations/how-to-automate-twitter-replies-using-the-api
