Code Examples
Ready-to-use code samples in popular programming languages.
PHP
Using cURL
<?php
// API Configuration
$apiToken = 'YOUR_API_TOKEN';
$baseUrl = 'http://localhost:8000/api/v1';
// Get posts by username
$platform = 'tiktok';
$username = 'charlidamelio';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "{$baseUrl}/posts/by-user/{$platform}/{$username}?limit=20",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$apiToken}",
"Accept: application/json"
]
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($response, true);
if ($data['success']) {
echo "Found {$data['data']['posts_count']} posts\n";
echo "Remaining balance: {$data['data']['remaining_balance']}\n\n";
foreach ($data['data']['posts'] as $post) {
echo "Post: {$post['content']}\n";
echo "Likes: {$post['engagement']['likes']}\n";
echo "URL: {$post['post_url']}\n\n";
}
} else {
echo "Error: {$data['error']['message']}\n";
}
Using Guzzle
<?php
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'http://localhost:8000/api/v1/',
'headers' => [
'Authorization' => 'Bearer YOUR_API_TOKEN',
'Accept' => 'application/json'
]
]);
try {
$response = $client->get('posts/by-hashtag/instagram/travel', [
'query' => [
'limit' => 30,
'min_likes' => 1000
]
]);
$data = json_decode($response->getBody(), true);
foreach ($data['data']['posts'] as $post) {
// Process each post
}
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
Python
Using requests
import requests
# API Configuration
API_TOKEN = 'YOUR_API_TOKEN'
BASE_URL = 'http://localhost:8000/api/v1'
headers = {
'Authorization': f'Bearer {API_TOKEN}',
'Accept': 'application/json'
}
# Get posts by username
platform = 'tiktok'
username = 'charlidamelio'
response = requests.get(
f'{BASE_URL}/posts/by-user/{platform}/{username}',
headers=headers,
params={
'limit': 20,
'min_likes': 100000
}
)
data = response.json()
if data['success']:
print(f"Found {data['data']['posts_count']} posts")
print(f"Remaining balance: {data['data']['remaining_balance']}")
for post in data['data']['posts']:
print(f"\nPost: {post['content'][:50]}...")
print(f"Likes: {post['engagement']['likes']:,}")
print(f"URL: {post['post_url']}")
else:
print(f"Error: {data['error']['message']}")
Async with aiohttp
import aiohttp
import asyncio
async def fetch_posts(session, platform, username):
url = f'http://localhost:8000/api/v1/posts/by-user/{platform}/{username}'
headers = {'Authorization': 'Bearer YOUR_API_TOKEN'}
async with session.get(url, headers=headers) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
# Fetch from multiple users concurrently
tasks = [
fetch_posts(session, 'tiktok', 'user1'),
fetch_posts(session, 'instagram', 'user2'),
fetch_posts(session, 'twitter', 'user3'),
]
results = await asyncio.gather(*tasks)
for result in results:
if result['success']:
print(f"Got {result['data']['posts_count']} posts")
asyncio.run(main())
JavaScript
Using fetch (Browser/Node.js)
const API_TOKEN = 'YOUR_API_TOKEN';
const BASE_URL = 'http://localhost:8000/api/v1';
async function getPostsByUser(platform, username, options = {}) {
const params = new URLSearchParams({
limit: options.limit || 50,
...options
});
const response = await fetch(
`${BASE_URL}/posts/by-user/${platform}/${username}?${params}`,
{
headers: {
'Authorization': `Bearer ${API_TOKEN}`,
'Accept': 'application/json'
}
}
);
return response.json();
}
// Usage
getPostsByUser('tiktok', 'charlidamelio', { limit: 20, min_likes: 100000 })
.then(data => {
if (data.success) {
console.log(`Found ${data.data.posts_count} posts`);
console.log(`Balance: ${data.data.remaining_balance}`);
data.data.posts.forEach(post => {
console.log(`${post.content} - ${post.engagement.likes} likes`);
});
} else {
console.error(`Error: ${data.error.message}`);
}
})
.catch(err => console.error('Request failed:', err));
Using axios
const axios = require('axios');
const api = axios.create({
baseURL: 'http://localhost:8000/api/v1',
headers: {
'Authorization': 'Bearer YOUR_API_TOKEN'
}
});
// Get posts by hashtag
api.get('/posts/by-hashtag/instagram/photography', {
params: {
limit: 25,
media_type: 'image'
}
})
.then(({ data }) => {
if (data.success) {
data.data.posts.forEach(post => {
console.log(post.author.username, post.engagement.likes);
});
}
})
.catch(error => {
console.error(error.response?.data?.error?.message || error.message);
});
cURL
Get Posts by Username
curl -X GET "http://localhost:8000/api/v1/posts/by-user/tiktok/charlidamelio?limit=20" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Accept: application/json"
Get Posts by Hashtag
curl -X GET "http://localhost:8000/api/v1/posts/by-hashtag/instagram/travel?limit=30&min_likes=1000" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Accept: application/json"
Advanced Search
curl -X GET "http://localhost:8000/api/v1/posts/search?platform=twitter&keyword=AI&from_date=2024-01-01&min_likes=500" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Accept: application/json"
Check Account Balance
curl -X GET "http://localhost:8000/api/v1/account" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Accept: application/json"