← Social API

Use the Social Media API with JavaScript

Call the Social API from a Node.js server with built-in fetch. Keep authentication server-side and save the request before sending so a timeout does not become a second paid request.

Use a server runtime

Run the example as a .mjs file in a current Node.js runtime with built-in fetch and AbortSignal.timeout. Set TICKERTRENDS_API_KEY in your server environment. Do not embed it in browser JavaScript, a public repository or a shared prompt.

Save once and resume safely

The script creates social-request.json only if it does not exist, then reuses the stored body and UUID. Each HTTP request has a 60-second timeout and polling stops after two minutes. Keep the file after an uncertain failure. Use a different receipt file for a different query; changing the body with the same key returns 409.

Use the same client for web search

For web search, use a separate receipt file and POST /web-search with {"query":"openai","country":"US","language":"en","limit":10,"offset":0,"max_credits":0.075}. Poll /web-search-requests/{id} every second and read results instead of posts after success. Follow pagination.next_offset with a new receipt when pagination.has_more is true. The same key and balance apply: 0.075 credit per started block of 10 returned results, or $0.75 per 1,000 searches returning 1–10 results each at the standard credit price. This endpoint is a rollout preview; handle HTTP 503 with bounded retries using the saved body and key.

Check the result before displaying it

A successful HTTP response may still describe processing. Use posts only after checking data.status and preserve source URLs and coverage caveats. Show a pending state when the polling deadline expires. Handle insufficient balance in the account workspace rather than automatically purchasing credits.

Server-side JavaScript quickstart

Example requests require your own API key. Running a request can consume prepaid credits within the stated budget.

import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { randomUUID } from 'node:crypto';
const base = 'https://social.tickertrends.io/api/public';
const receipt = 'social-request.json';
if (!existsSync(receipt)) {
  writeFileSync(receipt, JSON.stringify({ key: randomUUID(), body: {
    term: 'openai', platforms: 'tiktok', limit: 20, max_credits: 1
  }}), { flag: 'wx' });
}
const saved = JSON.parse(readFileSync(receipt, 'utf8'));
const headers = { 'X-API-Key': process.env.TICKERTRENDS_API_KEY,
  'Idempotency-Key': saved.key, 'Content-Type': 'application/json' };
async function request(path, options = {}) {
  const response = await fetch(base + path, { ...options, headers,
    signal: AbortSignal.timeout(60000) });
  if (!response.ok) throw new Error('HTTP ' + response.status);
  return (await response.json()).data;
}
let data = await request('/social-posts', {
  method: 'POST', body: JSON.stringify(saved.body)
});
const requestId = data.id;
const deadline = Date.now() + 120000;
while (data.status === 'processing' && Date.now() < deadline) {
  await new Promise(resolve => setTimeout(resolve, 5000));
  data = await request('/social-requests/' + encodeURIComponent(requestId));
}
console.log(JSON.stringify(data, null, 2));
// Retain the receipt to resume. Check status before using posts.