API Quickstart
Last updated
Last updated
Making your first request with the Athena API is straightforward with this script. It begins by defining the necessary ATHENA_API_KEY
and setting up the headers
. The payload
dictionary specifies the query parameters, including a search term ("donald trump") and other configuration details to filter the results (for the sake of simplicity we are choosing to omit extra parameters).
The initial request is sent via a POST
call to Athena’s API endpoint, with the response stored in data
. This response includes metadata such as totalResults
, allowing for efficient pagination. The loop then iterates over pages, adjusting the payload to request the next set of articles and appending each batch to all_articles
. By the end, all_articles
contains all retrieved articles matching the query, providing an easy way to gather a complete dataset in only a few lines of code.
import requests
import json
ATHENA_API_KEY = "your_api_key"
url = "https://app.runathena.com/api/query"
headers = {
"Content-Type": "application/json"
}
# Define the initial payload with the query and other required fields
payload = {
"query": "donald trump",
"api_key": ATHENA_API_KEY
}
# Send the initial request
response = requests.post(url, headers=headers, json=payload)
data = response.json()
# Initialize variables for pagination
page = 1
all_articles = []
articles_per_request = 25
# Fetch and collect all articles while there are more pages
while page * articles_per_request < data.get('totalResults', 0):
payload['page'] = page
response = requests.post(url, headers=headers, json=payload)
data = response.json()
all_articles.extend(data.get('articles', []))
page += 1
print(all_articles)
const axios = require('axios');
const ATHENA_API_KEY = "your_api_key";
const url = "https://app.runathena.com/api/query";
const headers = {
"Content-Type": "application/json"
};
// Define the initial payload with the query and other required fields
const payload = {
query: "donald trump",
api_key: ATHENA_API_KEY
};
// Fetch all articles function
const fetchArticles = async () => {
try {
// Send the initial request
let response = await axios.post(url, payload, { headers });
let data = response.data;
// Initialize variables for pagination
let page = 1;
const allArticles = [];
const articlesPerRequest = 25;
// Fetch and collect all articles while there are more pages
while (page * articlesPerRequest < (data.totalResults || 0)) {
payload.page = page; // Update the payload with the current page
response = await axios.post(url, payload, { headers });
data = response.data;
if (data.articles) {
allArticles.push(...data.articles); // Collect articles
}
page++;
}
console.log(allArticles);
} catch (error) {
console.error("Error fetching articles:", error.message);
}
};
// Call the function to fetch articles
fetchArticles();
require 'net/http'
require 'json'
ATHENA_API_KEY = "your_api_key"
url = URI("https://app.runathena.com/api/query")
# Define the initial payload with the query and other required fields
payload = {
query: "donald trump",
api_key: ATHENA_API_KEY
}
headers = {
"Content-Type" => "application/json"
}
# Fetch all articles function
def fetch_articles(url, headers, payload)
all_articles = []
articles_per_request = 25
page = 1
begin
# Send the initial request
response = Net::HTTP.post(url, payload.to_json, headers)
data = JSON.parse(response.body)
total_results = data['totalResults'] || 0
# Fetch and collect all articles while there are more pages
while page * articles_per_request < total_results
payload[:page] = page
response = Net::HTTP.post(url, payload.to_json, headers)
data = JSON.parse(response.body)
if data['articles']
all_articles.concat(data['articles'])
end
page += 1
end
puts all_articles
rescue StandardError => e
puts "Error fetching articles: #{e.message}"
end
end
# Call the function to fetch articles
fetch_articles(url, headers, payload)
// Some code<?php
$athenaApiKey = "your_api_key";
$url = "https://app.runathena.com/api/query";
// Define the initial payload with the query and other required fields
$payload = [
"query" => "donald trump",
"api_key" => $athenaApiKey
];
// Function to fetch articles
function fetchArticles($url, $payload)
{
$allArticles = [];
$articlesPerRequest = 25;
$page = 1;
try {
// Initialize cURL
$ch = curl_init();
// Function to execute cURL request
$executeCurl = function ($url, $payload) use ($ch) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
return json_decode($response, true);
};
// Fetch the first set of data
$data = $executeCurl($url, $payload);
$totalResults = $data['totalResults'] ?? 0;
// Fetch and collect all articles while there are more pages
while ($page * $articlesPerRequest < $totalResults) {
$payload['page'] = $page;
$data = $executeCurl($url, $payload);
if (!empty($data['articles'])) {
$allArticles = array_merge($allArticles, $data['articles']);
}
$page++;
}
curl_close($ch);
// Print all articles
print_r($allArticles);
} catch (Exception $e) {
echo "Error fetching articles: " . $e->getMessage();
}
}
// Call the function to fetch articles
fetchArticles($url, $payload);
?>
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
public class AthenaQuery {
private static final String ATHENA_API_KEY = "your_api_key";
private static final String URL = "https://app.runathena.com/api/query";
public static void main(String[] args) {
try {
fetchArticles();
} catch (Exception e) {
System.err.println("Error fetching articles: " + e.getMessage());
}
}
public static void fetchArticles() throws Exception {
HttpClient client = HttpClient.newHttpClient();
ObjectMapper objectMapper = new ObjectMapper();
// Define the initial payload
String payload = objectMapper.writeValueAsString(new Payload("donald trump", ATHENA_API_KEY, 0));
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
// Send the initial request
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonNode rootNode = objectMapper.readTree(response.body());
int totalResults = rootNode.path("totalResults").asInt();
int articlesPerRequest = 25;
int page = 1;
List<JsonNode> allArticles = new ArrayList<>();
// Fetch and collect all articles while there are more pages
while (page * articlesPerRequest < totalResults) {
payload = objectMapper.writeValueAsString(new Payload("donald trump", ATHENA_API_KEY, page));
request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
response = client.send(request, HttpResponse.BodyHandlers.ofString());
rootNode = objectMapper.readTree(response.body());
if (rootNode.has("articles")) {
for (JsonNode article : rootNode.path("articles")) {
allArticles.add(article);
}
}
page++;
}
// Print all articles
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(allArticles));
}
// Helper class for the payload
static class Payload {
public String query;
public String api_key;
public int page;
public Payload(String query, String api_key, int page) {
this.query = query;
this.api_key = api_key;
this.page = page;
}
}
}