Exa Search
Search Exa for information about people, companies, or any topic. Returns structured results with titles, URLs, and content snippets.
Source Code
const [query, numResults = "5", type = "auto"] = process.argv.slice(2);
if (!query) {
console.error("Error: query is required");
process.exit(1);
}
const numResultsInt = parseInt(numResults, 10);
console.log(`Searching Exa: "${query}" (${numResultsInt} results, ${type})`);
try {
const response = await fetch("https://api.exa.ai/search", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "PLACEHOLDER_TOKEN",
},
body: JSON.stringify({
query,
numResults: numResultsInt,
type,
contents: {
text: { maxCharacters: 1500 },
highlights: { numSentences: 3 },
},
}),
});
if (!response.ok) {
console.error(`Exa API failed: ${response.status}`);
console.error(await response.text());
throw new Error(`Exa search failed: ${response.status}`);
}
const data = await response.json();
if (!data.results || data.results.length === 0) {
console.log("No results found");
console.log(JSON.stringify({ results: [] }, null, 2));
process.exit(0);
}
const results = data.results.map((r) => ({
title: r.title || "Untitled",
url: r.url,
publishedDate: r.publishedDate || null,
author: r.author || null,
text: r.text || "",
highlights: r.highlights || [],
}));
console.log(`✓ Found ${results.length} results`);
console.log(JSON.stringify({ results }, null, 2));
} catch (error) {
console.error("Error searching Exa:", error.message);
throw error;
} const [query, numResults = "5", type = "auto"] = process.argv.slice(2);
if (!query) {
console.error("Error: query is required");
process.exit(1);
}
const numResultsInt = parseInt(numResults, 10);
console.log(`Searching Exa: "${query}" (${numResultsInt} results, ${type})`);
try {
const response = await fetch("https://api.exa.ai/search", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "PLACEHOLDER_TOKEN",
},
body: JSON.stringify({
query,
numResults: numResultsInt,
type,
contents: {
text: { maxCharacters: 1500 },
highlights: { numSentences: 3 },
},
}),
});
if (!response.ok) {
console.error(`Exa API failed: ${response.status}`);
console.error(await response.text());
throw new Error(`Exa search failed: ${response.status}`);
}
const data = await response.json();
if (!data.results || data.results.length === 0) {
console.log("No results found");
console.log(JSON.stringify({ results: [] }, null, 2));
process.exit(0);
}
const results = data.results.map((r) => ({
title: r.title || "Untitled",
url: r.url,
publishedDate: r.publishedDate || null,
author: r.author || null,
text: r.text || "",
highlights: r.highlights || [],
}));
console.log(`✓ Found ${results.length} results`);
console.log(JSON.stringify({ results }, null, 2));
} catch (error) {
console.error("Error searching Exa:", error.message);
throw error;
}