JavaScript / Node
JavaScript / Node.js
Use the built-in fetch API (Node.js 18+) or any HTTP client. You can also install the official JavaScript SDK: npm install @xcrop/sdk
Helper Function
xcrop.jsJAVASCRIPT
const API_KEY = process.env.XCROP_API_KEY const BASE = 'https://xcrop.io/api' async function xcrop(path, options = {}) { const { method = 'GET', body } = options const res = await fetch(BASE + path, { method, headers: { 'Authorization': 'Bearer ' + API_KEY, ...(body ? { 'Content-Type': 'application/json' } : {}), }, ...(body ? { body: JSON.stringify(body) } : {}), }) if (!res.ok) throw new Error('XCROP ' + res.status + ': ' + (await res.text())) return res.json() }
Search Tweets
JAVASCRIPT
const result = await xcrop('/v2/search', { method: 'POST', body: { query: '#bitcoin', limit: 20, sort: 'latest' }, }) for (const tweet of result.data) { console.log('@' + tweet.author.username + ': ' + tweet.text) console.log(' Likes: ' + tweet.metrics.likes + ' | RT: ' + tweet.metrics.retweets) }
User Profile + Tweets
JAVASCRIPT
// Get user profile const { data: user } = await xcrop('/v2/users/elonmusk') console.log(user.name + ' — ' + user.followers + ' followers') // Get user's recent tweets const { data: tweets } = await xcrop('/v2/users/elonmusk/tweets?count=10') tweets.forEach(t => console.log(t.text.slice(0, 80)))
Create a Tweet (Write API)
JAVASCRIPT
const { data } = await xcrop('/v2/tweets/create', { method: 'POST', body: { text: 'Hello from XCROP API!' }, }) console.log('Posted:', data.url)