home / skills / openclaw / skills / twitter-x-api

twitter-x-api skill

/skills/klemenska/twitter-x-api

This skill lets you manage X/Twitter via API to post, read, reply, search, and analyze engagement.

npx playbooks add skill openclaw/skills --skill twitter-x-api

Review the files below or copy the command above to add this skill to your agents.

Files (5)
SKILL.md
4.9 KB
---
name: twitter
description: "X/Twitter messaging management via API. Read tweets, post tweets, reply, send DMs, search, and view analytics. Use when the user wants to interact with X/Twitter: (1) posting or scheduling tweets, (2) reading timeline/mentions/DMs, (3) replying to tweets, (4) searching tweets/users/hashtags, (5) checking engagement analytics. Requires Twitter API credentials (API key, API secret, access token, access secret) or Bearer token."
---

# Twitter/X API Skill

Interact with X (Twitter) via API v2 for reading, posting, replying, DMs, search, and analytics.

## Setup

### Credentials

Store credentials in environment variables or `~/.config/twitter/credentials.json`:

```bash
export TWITTER_API_KEY="your-api-key"
export TWITTER_API_SECRET="your-api-secret"
export TWITTER_ACCESS_TOKEN="your-access-token"
export TWITTER_ACCESS_SECRET="your-access-secret"
export TWITTER_BEARER_TOKEN="your-bearer-token"  # For read-only operations
```

Or create credentials file:

```bash
mkdir -p ~/.config/twitter
cat > ~/.config/twitter/credentials.json << 'EOF'
{
  "api_key": "your-api-key",
  "api_secret": "your-api-secret",
  "access_token": "your-access-token",
  "access_secret": "your-access-secret",
  "bearer_token": "your-bearer-token"
}
EOF
chmod 600 ~/.config/twitter/credentials.json
```

### Install Dependencies

```bash
pip install tweepy
```

## Quick Reference

| Task | Command |
|------|---------|
| Post tweet | `{baseDir}/scripts/tweet.py post "text"` |
| Post with image | `{baseDir}/scripts/tweet.py post "text" --media image.png` |
| Reply | `{baseDir}/scripts/tweet.py reply TWEET_ID "text"` |
| Thread | `{baseDir}/scripts/tweet.py thread "tweet1" "tweet2" ...` |
| Get timeline | `{baseDir}/scripts/tweet.py timeline [--count 20]` |
| Get mentions | `{baseDir}/scripts/tweet.py mentions [--count 20]` |
| Get DMs | `{baseDir}/scripts/tweet.py dms [--count 20]` |
| Send DM | `{baseDir}/scripts/tweet.py dm USERNAME "message"` |
| Search | `{baseDir}/scripts/tweet.py search "query" [--count 20]` |
| User info | `{baseDir}/scripts/tweet.py user USERNAME` |
| Tweet info | `{baseDir}/scripts/tweet.py show TWEET_ID` |
| Analytics | `{baseDir}/scripts/tweet.py analytics TWEET_ID` |

## Scripts

### tweet.py

Main script for all Twitter operations. Run with `--help` for details:

```bash
{baseDir}/scripts/tweet.py --help
{baseDir}/scripts/tweet.py post --help
```

### Common Workflows

**Post a simple tweet:**

```bash
{baseDir}/scripts/tweet.py post "Hello, world!"
```

**Post with image:**

```bash
{baseDir}/scripts/tweet.py post "Check this out!" --media photo.png
{baseDir}/scripts/tweet.py post "Multiple images" --media img1.png --media img2.png
```

**Reply to a tweet:**

```bash
{baseDir}/scripts/tweet.py reply 1234567890 "Great point!"
```

**Post a thread:**

```bash
{baseDir}/scripts/tweet.py thread \
  "First tweet in thread" \
  "Second tweet" \
  "Third tweet"
```

**Read your mentions:**

```bash
{baseDir}/scripts/tweet.py mentions --count 50
```

**Search for tweets:**

```bash
{baseDir}/scripts/tweet.py search "openclaw agent" --count 20
{baseDir}/scripts/tweet.py search "#AI lang:en" --count 20
```

**Get user info:**

```bash
{baseDir}/scripts/tweet.py user elonmusk
```

**Send a DM:**

```bash
{baseDir}/scripts/tweet.py dm username "Hello from OpenClaw!"
```

**View tweet analytics:**

```bash
{baseDir}/scripts/tweet.py analytics 1234567890
```

## API Tiers & Limits

| Tier | Cost | Read | Write | Search |
|------|------|------|-------|--------|
| Free | $0 | Limited | - | - |
| Basic | $100/mo | 10k/mo | 1.5k/mo | 50/mo |
| Pro | $5000/mo | 1M/mo | 300k/mo | 500/mo |

**Free tier** can only post tweets (no read access to timeline/mentions).
**Basic tier** required for reading mentions, timeline, and search.
**Write-only operations** work on free tier.

See `{baseDir}/references/api-limits.md` for detailed rate limits.

## Error Handling

Common errors:

| Error | Cause | Solution |
|-------|-------|----------|
| 403 Forbidden | Insufficient tier | Upgrade API tier or check endpoint access |
| 429 Too Many Requests | Rate limit hit | Wait and retry; check rate limit headers |
| 401 Unauthorized | Invalid credentials | Verify API keys and tokens |
| 404 Not Found | Tweet/user deleted | Handle gracefully, inform user |
| 422 Unprocessable | Duplicate tweet | Wait before posting same content |

## Notes

- **Rate limits**: X API has strict rate limits. Scripts include retry logic.
- **Media uploads**: Images must be <5MB (PNG/JPG) or <15MB (GIF). Videos <512MB.
- **Character limit**: 280 characters per tweet. Threads for longer content.
- **DMs**: Require OAuth 1.0a user context (not Bearer token).
- **Search operators**: `{baseDir}/references/search-operators.md` for advanced queries.

## Related Files

- `{baseDir}/scripts/tweet.py` - Main CLI for all operations
- `{baseDir}/references/api-limits.md` - Detailed rate limits by endpoint
- `{baseDir}/references/search-operators.md` - Twitter search syntax

Overview

This skill provides complete X/Twitter messaging management via the Twitter API v2. It lets you read timelines, post tweets, reply, send direct messages, search tweets/users/hashtags, and retrieve engagement analytics. Credentials (API key/secret and access tokens or bearer token) are required to enable the appropriate operations. It supports common workflows like posting, threading, replying, searching, and viewing analytics.

How this skill works

The skill connects to X/Twitter using provided API credentials and the tweepy Python client to call v2 endpoints. It exposes CLI-style commands for posting tweets (with media), replying, creating threads, reading timelines/mentions/DMs, searching, fetching user/tweet details, and pulling tweet analytics. The implementation respects API tiers and rate limits, includes retry logic for transient failures, and requires OAuth 1.0a user context for DM/send operations.

When to use it

  • Post, schedule, or bulk-publish tweets and image/media attachments.
  • Read your timeline, mentions, or direct messages programmatically.
  • Reply to tweets or publish threaded content longer than 280 characters.
  • Search for tweets, users, or hashtags with advanced operators.
  • Retrieve engagement and analytics for specific tweets to measure performance.

Best practices

  • Store credentials securely (environment variables or a protected config file) and avoid committing keys to source control.
  • Use the bearer token for read-only operations; use OAuth 1.0a credentials for DMs and write actions when required.
  • Respect API rate limits and design retries with exponential backoff when receiving 429 or transient errors.
  • Keep media files within platform limits (images <5MB, GIFs <15MB, videos <512MB) and resize before upload.
  • Avoid posting duplicate content to prevent 422 errors; batch or schedule posts to stay under write quotas.

Example use cases

  • Post a single tweet or multiple-image tweet from a script or CI pipeline.
  • Monitor mentions and automatically reply or triage customer support messages.
  • Search recent tweets about a campaign hashtag and pull engagement metrics for reporting.
  • Send targeted DMs to users during a promo using OAuth user credentials.
  • Archive timeline or mentions periodically for backup and analysis.

FAQ

What credentials are required?

Provide API key, API secret, access token, and access secret for full read/write and DM capabilities; a bearer token suffices for many read-only operations.

Can I post media with tweets?

Yes. The skill supports image and video uploads within Twitter's size limits; multiple images and GIFs are supported per API rules.

What happens when I hit rate limits?

The scripts include retry logic, but you should implement exponential backoff and upgrade your API tier or reduce request frequency if limits are regularly reached.