# Search X By Keywords (`watcher.data/search-x-by-keywords`) Actor

Fast and reliable scraper for searching tweets and users on X (Twitter). Supports multiple output formats (JSON, CSV, Excel), advanced filtering options, and real-time data extraction. Perfect for social media monitoring, research, and business intelligence.

- **URL**: https://apify.com/watcher.data/search-x-by-keywords.md
- **Developed by:** [Giichi Arai](https://apify.com/watcher.data) (community)
- **Categories:** Social media, Integrations, Automation
- **Features:** Limited permissions
- **Stats:** 1,318 total users, 102 monthly users, 100.0% runs succeeded, 4 bookmarks
- **User rating**: 4.00 out of 5 stars

## Pricing

$3.00 / 1,000 results

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## Search X (Twitter) by Keywords – Fast API-powered Scraper

This Apify Actor allows you to search for tweets and users on X (Twitter) using a fast API. It's designed to be efficient, user-friendly, and supports multiple output formats.

### Features

- 🔍 **Dual Search Types**: Search for tweets or users
- 🎯 **Multiple Keywords**: Support for multiple keywords with separate API calls for maximum coverage
- ⚡ **Fast API Integration**: Uses optimized API endpoints for quick results
- 📊 **Multiple Output Formats**: JSON, CSV, and Excel (XLSX)
- 🔧 **Flexible Sorting**: Latest, popular, or mixed result ordering
- 🔄 **Retry Logic**: Built-in retry mechanism with exponential backoff
- 📈 **Pagination Support**: Handles large result sets efficiently
- 🛡️ **Proxy Support**: Built-in proxy configuration for reliability

### Input Configuration

#### Required Fields

- **Search Type**: Choose between searching for tweets or users
- **Keywords**: At least one keyword is required
- **Max Items Per Keyword**: Number of results per keyword (1-200)

#### Search Keywords Options

- **Keywords**: Array of keywords to search for (hashtags, usernames, phrases, etc.) - each will be searched separately via individual API calls
- **Max Items Per Keyword**: Number of results to retrieve for each individual keyword (final results will be deduplicated across keywords)

#### Optional Fields

##### Tweet Search Options

- **Include Replies**: Include reply tweets in results (client-side filtering)
- **Include Retweets**: Include retweets in results (client-side filtering)
- **Sort By**: Sort results by latest, popular, or mixed

##### Filtering Options

- **Sorting**: Sort results by latest, popular, or mixed

##### Output Options

- **Output Format**: Choose JSON, CSV, or Excel format
- **Proxy Configuration**: Configure proxy settings

### Usage Examples

#### Search for Tweets about AI (Multiple Keywords)

```json
{
  "searchType": "tweets",
  "keywords": ["#AI", "MachineLearning", "ArtificialIntelligence", "DeepLearning", "NeuralNetworks"],
  "maxItemsPerKeyword": 50,
  "sortBy": "latest",
  "outputFormat": "json"
}
```

**Note**: This will make 5 separate API calls (one for each keyword), getting 50 tweets per keyword, then deduplicate the combined results.

#### Search for Tech Users

```json
{
  "searchType": "users",
  "keywords": ["tech entrepreneur", "startup", "CEO", "founder", "developer"],
  "maxItemsPerKeyword": 20,
  "outputFormat": "csv"
}
```

**Note**: This will make 5 separate API calls (one for each keyword), getting 20 users per keyword, then deduplicate the combined results.

#### Search for Cryptocurrency Tweets

```json
{
  "searchType": "tweets",
  "keywords": ["cryptocurrency", "Bitcoin", "Ethereum", "blockchain", "DeFi"],
  "maxItemsPerKeyword": 50,
  "sortBy": "popular",
  "outputFormat": "excel"
}
```

**Note**: This will make 5 separate searches (one for each keyword), getting 50 tweets per keyword, then deduplicate the combined results.

### Output Schema

#### Tweet Data

Each tweet result includes:

- `id`: Unique tweet identifier

- `text`: Tweet content

- `author`: Username of the author

- `author_name`: Display name of the author

- `created_at`: Tweet creation timestamp

- `retweet_count`: Number of retweets

- `like_count`: Number of likes

- `reply_count`: Number of replies

- `quote_count`: Number of quotes

- `is_retweet`: Whether this is a retweet

- `is_reply`: Whether this is a reply

- `hashtags`: Array of hashtags used

- `mentions`: Array of mentioned users

- `urls`: Array of URLs in the tweet

- `url`: Direct link to the tweet

#### User Data

Each user result includes:

- `id`: Unique user identifier
- `username`: User's handle (@username)
- `name`: Display name
- `description`: User bio/description
- `followers_count`: Number of followers
- `following_count`: Number of accounts following
- `tweets_count`: Total number of tweets
- `created_at`: Account creation date
- `verified`: Verification status
- `location`: User's location
- `profile_image_url`: Profile picture URL
- `profile_url`: Profile page URL

### Performance & Limitations

- **Speed**: Execution time depends on number of keywords (1 second delay between API calls)
- **Multiple API Calls**: Each keyword triggers a separate API call for maximum results
- **Rate Limiting**: Built-in retry logic and delays between requests
- **Data Freshness**: Results are fetched in real-time from the API
- **Deduplication**: Automatic removal of duplicate tweets/users across keyword results
- **Per-Keyword Control**: Precise control over results per keyword (up to 200 per keyword)

### Error Handling

The Actor includes comprehensive error handling:

- **API Failures**: Automatic retry with exponential backoff
- **Invalid Queries**: Clear error messages for malformed inputs
- **Network Issues**: Graceful handling of connection problems
- **Data Format Errors**: Robust parsing of API responses

### Technical Details

- **Runtime**: Python 3.11
- **Framework**: Apify SDK for Python
- **API Integration**: HTTP requests with session management
- **Data Processing**: Pandas for CSV/Excel export
- **Logging**: Comprehensive logging for debugging

### Support

For issues or questions:

1. Check the Actor logs for detailed error information
2. Ensure your search keywords are properly formatted
3. Verify API connectivity and quotas
4. Contact support with specific error messages

### Version History

- **v1.0.0**: Initial release with tweet and user search functionality
- Support for multiple output formats
- Built-in retry logic and error handling
- Comprehensive input validation

***

*This Actor is designed for legitimate research, marketing, and business intelligence purposes. Please comply with X (Twitter) Terms of Service and applicable laws.*

# Actor input Schema

## `searchType` (type: `string`):

Choose what to search for

## `keywords` (type: `array`):

Enter keywords to search for (hashtags, usernames, phrases, etc.). Each keyword will be searched separately via individual API calls.

## `maxItemsPerKeyword` (type: `integer`):

Maximum number of items to retrieve for each individual keyword (after deduplication across keywords)

## `sortBy` (type: `string`):

How to sort the results (for tweets only)

## `outputFormat` (type: `string`):

Choose the output format for downloaded data

## `proxyConfiguration` (type: `object`):

Proxy settings for requests

## Actor input object example

```json
{
  "searchType": "tweets",
  "keywords": [
    "#AI",
    "Python"
  ],
  "maxItemsPerKeyword": 50,
  "sortBy": "latest",
  "outputFormat": "json",
  "proxyConfiguration": {
    "useApifyProxy": false
  }
}
```

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "keywords": [
        "#AI",
        "Python"
    ],
    "proxyConfiguration": {
        "useApifyProxy": false
    }
};

// Run the Actor and wait for it to finish
const run = await client.actor("watcher.data/search-x-by-keywords").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "keywords": [
        "#AI",
        "Python",
    ],
    "proxyConfiguration": { "useApifyProxy": False },
}

# Run the Actor and wait for it to finish
run = client.actor("watcher.data/search-x-by-keywords").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "keywords": [
    "#AI",
    "Python"
  ],
  "proxyConfiguration": {
    "useApifyProxy": false
  }
}' |
apify call watcher.data/search-x-by-keywords --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,watcher.data/search-x-by-keywords"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/8CiMefkv2yLlD7vYl/builds/ikGQJK6SXcjLnUNU2/openapi.json
