Skip to main content
Follow these recommendations to get the most out of Xpoz’s API, whether you are building an AI agent, a data pipeline, or an interactive application.

Use field selection

Always specify the fields parameter to retrieve only the data you need. This dramatically improves response time and reduces memory usage.
// Slow: returns all 20+ fields per post
const results = await client.twitter.searchPosts('AI');

// Fast: returns only what you need
const results = await client.twitter.searchPosts('AI', {
  fields: ['id', 'text', 'likeCount', 'authorUsername', 'createdAtDate'],
});
For engagement analysis, use ["id", "text", "likeCount", "retweetCount", "replyCount", "createdAtDate"]. For user discovery, use ["id", "username", "name", "followersCount", "description"].

Choose the right response mode

ModeWhen to useRows returned
Fast (default)Quick lookups, UI previews, agent queriesUp to 300
PagingIterating through full result sets100 per page, unlimited total
CSVBulk exports, data analysis, archivalUp to 500K
import { ResponseType } from '@xpoz/xpoz';

// Quick lookup: get top 50 results immediately
const fast = await client.twitter.searchPosts('AI', {
  responseType: ResponseType.Fast,
  limit: 50,
});

// Full iteration: paginate through all results
const paged = await client.twitter.searchPosts('AI', {
  responseType: ResponseType.Paging,
});

// Bulk export: download as CSV
const csv = await client.twitter.searchPosts('AI', {
  responseType: ResponseType.Csv,
});
const url = await csv.exportCsv();

Pagination patterns

Do not fetch all pages unless you actually need the full dataset. Common patterns:

Sample the first page, then decide

const results = await client.twitter.searchPosts('bitcoin', {
  responseType: ResponseType.Paging,
});

console.log(`Total: ${results.pagination.totalRows} results`);

// Only fetch more if needed
if (results.pagination.totalRows < 500) {
  let page = results;
  while (page.hasNextPage()) {
    page = await page.nextPage();
    // process page.data
  }
} else {
  // Too many results -- export to CSV instead
  const csvUrl = await results.exportCsv();
}

Jump to a specific page

const page5 = await results.getPage(5);
page5 = results.get_page(5)

Query optimization

Be specific with keywords

// Too broad: returns millions of results
"AI"

// Better: narrow with boolean operators
"AI" AND "healthcare" AND 2025

// Best: exact phrases with filters
"AI-powered diagnostics" AND (healthcare OR medical)

Use date ranges

Always include startDate (and optionally endDate) to limit results to a relevant time window:
const results = await client.twitter.searchPosts('AI', {
  startDate: '2025-06-01',
  endDate: '2025-06-15',
});

Use platform-specific filters

Do not embed filters in the query string. Use dedicated parameters:
ParameterTypeScriptPython
Author filterauthorUsername: "elonmusk"author_username="elonmusk"
Language filterlanguage: "en"language="en"
Subreddit filtersubreddit: "learnpython"subreddit="learnpython"
Reddit sortsort: "top"sort="top"
Reddit timetime: "month"time="month"

Error handling

Build retry logic for timeouts and transient failures:
import {
  XpozError,
  AuthenticationError,
  OperationTimeoutError,
  OperationFailedError,
} from '@xpoz/xpoz';

async function searchWithRetry(query: string, retries = 2) {
  for (let attempt = 0; attempt <= retries; attempt++) {
    try {
      return await client.twitter.searchPosts(query, {
        responseType: ResponseType.Fast,
        limit: 100,
      });
    } catch (e) {
      if (e instanceof AuthenticationError) {
        throw e; // Don't retry auth errors
      }
      if (e instanceof OperationTimeoutError && attempt < retries) {
        console.log(`Attempt ${attempt + 1} timed out, retrying...`);
        continue;
      }
      throw e;
    }
  }
}
Authentication errors (401) should never be retried. Timeout errors are safe to retry. Failed operations may indicate a server-side issue — check the operationError field for details.

Caching

Xpoz caches operation results server-side with a TTL:
  • Running operations: 30-minute TTL (auto-expire if stalled)
  • Completed operations: 15-minute TTL (retrieve results within this window)
  • Paginated tables: Remain available for page navigation after creation
For paging mode, the first call creates a server-side table with all results. Subsequent nextPage() and getPage(n) calls read from this cached table, making page navigation fast.

Client lifecycle

Always close the client when done to release resources:
// Option 1: async disposal (Node.js 18.2+)
await using client = new XpozClient({ apiKey: 'key' });
await client.connect();
// client.close() called automatically

// Option 2: try/finally
const client = new XpozClient({ apiKey: 'key' });
await client.connect();
try {
  const user = await client.twitter.getUser('elonmusk');
} finally {
  await client.close();
}

Summary checklist

  • Always specify fields to reduce response size
  • Use Fast mode for quick lookups, Paging for iteration, CSV for bulk export
  • Include date ranges to narrow results
  • Use dedicated filter parameters instead of embedding filters in queries
  • Handle AuthenticationError and OperationTimeoutError separately
  • Close the client when done

Next steps

Query Syntax

Master boolean operators and phrase matching

CSV Exports

Export large datasets for offline analysis

Field Selection

Full list of available fields per platform

MCP Tools

Browse all 48 available tools