The Search API provides powerful search capabilities across web and proprietary data sources, returning relevant content optimized for AI applications and RAG pipelines.

Basic Usage

import { Valyu } from "valyu-js";

const valyu = new Valyu();

const response = await valyu.search(
  "What are the latest developments in quantum computing?"
);

console.log(`Found ${response.results.length} results`);
response.results.forEach(result => {
  console.log(`Title: ${result.title}`);
  console.log(`URL: ${result.url}`);
  console.log(`Content: ${result.content.substring(0, 200)}...`);
});

Parameters

Query (Required)

ParameterTypeDescription
querystringThe search query string

Options (Optional)

ParameterTypeDescriptionDefault
searchType"web" | "proprietary" | "all"Type of search to perform. "web" for web search, "proprietary" for Valyu datasets, "all" for both"all"
maxNumResultsnumberMaximum number of results to return (1-20)10
maxPricenumberMaximum cost in dollars per thousand retrievals (CPM)30.0
isToolCallbooleanSet to true when called by AI agents/tools, false for direct user queriestrue
relevanceThresholdnumberMinimum relevance score for results (0.0-1.0)0.5
includedSourcesstring[]List of specific sources to search within (URLs, domains, or dataset names)undefined
excludeSourcesstring[]List of sources to exclude from search resultsundefined
categorystringNatural language category to guide search contextundefined
startDatestringStart date for time filtering (YYYY-MM-DD format)undefined
endDatestringEnd date for time filtering (YYYY-MM-DD format)undefined
countryCodestring2-letter ISO country code to bias search resultsundefined
responseLengthstring | numberContent length per result: "short" (25k), "medium" (50k), "large" (100k), "max" (full), or custom character count"short"
fastModebooleanEnable fast mode for reduced latency but shorter resultsfalse

Response Format

interface SearchResponse {
  success: boolean;
  error?: string;
  tx_id: string | null;
  query: string;
  results: SearchResult[];
  results_by_source: {
    web: number;
    proprietary: number;
  };
  total_deduction_pcm: number;
  total_deduction_dollars: number;
  total_characters: number;
}

interface SearchResult {
  title: string;
  url: string;
  content: string;
  description?: string;
  source: string;
  price: number;
  length: number;
  relevance_score: number;
  data_type?: "structured" | "unstructured";
  // Additional fields for academic sources
  publication_date?: string;
  authors?: string[];
  citation?: string;
  doi?: string;
  // ... other optional fields
}

Parameter Examples

Fast Mode

Enable fast mode for quicker results:
const response = await valyu.search(query, {
  fastMode: true,
});
Responses will be quicker but the content will be shorter.

Search Type Configuration

Control which data sources to search:
// Web search only
const webResponse = await valyu.search(query, {
  searchType: "web",
  maxNumResults: 10
});

// Proprietary datasets only
const proprietaryResponse = await valyu.search(query, {
  searchType: "proprietary",
  maxNumResults: 8
});

// Both web and proprietary (default)
const allResponse = await valyu.search(query, {
  searchType: "all",
  maxNumResults: 12
});

Source Filtering

Control which specific sources to include or exclude:
const response = await valyu.search(
  "quantum computing applications",
  {
    searchType: "all",
    maxNumResults: 10,
    includedSources: ["nature.com", "science.org", "valyu/valyu-arxiv"],
    responseLength: "medium"
  }
);
const response = await valyu.search(
  "quantum computing applications",
  {
    searchType: "all",
    maxNumResults: 10,
    excludeSources: ["reddit.com", "quora.com"],
    responseLength: "medium"
  }
);
You can either include or exclude sources, but not both.

Geographic and Date Filtering

Bias results by location and time range:
const response = await valyu.search(
  "renewable energy policies",
  {
    countryCode: "US",
    startDate: "2024-01-01",
    endDate: "2024-12-31",
    maxNumResults: 7,
    category: "government policy"
  }
);

Response Length Control

Customize content length per result:
// Predefined lengths
const shortResponse = await valyu.search(query, {
  responseLength: "short" // 25k characters
});

const mediumResponse = await valyu.search(query, {
  responseLength: "medium" // 50k characters
});

const largeResponse = await valyu.search(query, {
  responseLength: "large" // 100k characters
});

// Custom character limit
const customResponse = await valyu.search(query, {
  responseLength: 15000 // Custom limit
});

Use Case Examples

Academic Research Assistant

Build a comprehensive research tool that searches across academic databases:
async function academicResearch(query: string) {
  const response = await valyu.search(query, {
    searchType: "proprietary",
    includedSources: ["valyu/valyu-pubmed", "valyu/valyu-arxiv"],
    maxNumResults: 15,
    responseLength: "large",
    category: "academic research"
  });

  if (response.success) {
    console.log(`=== Academic Research Results ===`);
    console.log(`Found ${response.results.length} papers for: "${query}"`);
    
    // Group by source
    const arxivPapers = response.results.filter(r => r.source.includes("arxiv"));
    const pubmedPapers = response.results.filter(r => r.source.includes("pubmed"));
    
    console.log(`\nArXiv Papers: ${arxivPapers.length}`);
    arxivPapers.forEach((paper, i) => {
      console.log(`${i + 1}. ${paper.title}`);
      console.log(`   Relevance: ${paper.relevance_score.toFixed(2)}`);
      console.log(`   URL: ${paper.url}`);
      if (paper.publication_date) {
        console.log(`   Published: ${paper.publication_date}`);
      }
      if (paper.authors) {
        console.log(`   Authors: ${paper.authors.join(", ")}`);
      }
    });
    
    console.log(`\nPubMed Articles: ${pubmedPapers.length}`);
    pubmedPapers.forEach((article, i) => {
      console.log(`${i + 1}. ${article.title}`);
      console.log(`   Relevance: ${article.relevance_score.toFixed(2)}`);
      if (article.citation) {
        console.log(`   Citation: ${article.citation}`);
      }
    });
    
    return {
      arxiv: arxivPapers,
      pubmed: pubmedPapers,
      query
    };
  }
  
  return null;
}

// Usage examples - dates are included in natural language
const covidResearch = await academicResearch(
  "COVID-19 vaccine efficacy studies published between 2023 and 2024"
);

const aiResearch = await academicResearch(
  "recent machine learning breakthroughs in the last 2 years"
);

const climateResearch = await academicResearch(
  "climate change mitigation strategies peer-reviewed research since 2020"
);

Financial Market Intelligence

Create a financial analysis tool that searches market data and news:
async function financialIntelligence(query: string, analysisType: "fundamental" | "technical" | "news") {
  const sources = {
    fundamental: ["valyu/valyu-stocks-US", "sec.gov", "investor.com"],
    technical: ["valyu/valyu-stocks-US", "tradingview.com", "yahoo.com"],
    news: ["bloomberg.com", "cnbc.com", "reuters.com", "marketwatch.com"]
  };

  const response = await valyu.search(query, {
    searchType: analysisType === "fundamental" || analysisType === "technical" ? "all" : "web",
    includedSources: sources[analysisType],
    maxNumResults: 10,
    responseLength: "medium",
    category: "financial analysis"
  });

  if (response.success) {
    console.log(`=== ${analysisType.toUpperCase()} Analysis ===`);
    console.log(`Query: "${query}"`);
    
    response.results.forEach((result, i) => {
      console.log(`\n${i + 1}. ${result.title}`);
      console.log(`   Source: ${result.source}`);
      console.log(`   Relevance: ${result.relevance_score.toFixed(2)}`);
      console.log(`   URL: ${result.url}`);
      
      // Show excerpt for financial data
      if (result.content.length > 200) {
        console.log(`   Preview: ${result.content.substring(0, 200)}...`);
      }
      
      if (result.publication_date) {
        console.log(`   Date: ${result.publication_date}`);
      }
    });
    
    return {
      results: response.results,
      analysisType,
      query
    };
  }
  
  return null;
}

// Usage examples - include timeframes in natural language
const teslaFundamentals = await financialIntelligence(
  "Tesla financial performance Q3 2024 earnings revenue profit margins", 
  "fundamental"
);

const appleNews = await financialIntelligence(
  "Apple latest news this week product announcements stock updates", 
  "news"
);

const bitcoinTechnical = await financialIntelligence(
  "Bitcoin price analysis technical indicators support resistance levels recent trends",
  "technical"
);

Real-time News Monitoring

Build a news monitoring system that tracks specific topics across multiple sources:
async function newsMonitoring(queries: string[]) {
  const allResults = [];
  
  for (const query of queries) {
    const response = await valyu.search(query, {
      searchType: "web",
      includedSources: [
        "reuters.com",
        "bloomberg.com", 
        "cnbc.com",
        "techcrunch.com",
        "theverge.com"
      ],
      maxNumResults: 8,
      responseLength: "short"
    });

    if (response.success) {
      allResults.push({
        query,
        articles: response.results,
        count: response.results.length
      });
    }
  }

  // Generate monitoring report
  console.log(`=== News Monitoring Report ===`);
  console.log(`Monitoring ${queries.length} topics\n`);
  
  allResults.forEach(({ query, articles, count }) => {
    console.log(`📰 ${query.toUpperCase()}: ${count} articles`);
    
    articles.forEach((article, i) => {
      console.log(`   ${i + 1}. ${article.title}`);
      console.log(`      Source: ${article.source} | Relevance: ${article.relevance_score.toFixed(2)}`);
      console.log(`      URL: ${article.url}`);
    });
    console.log("");
  });

  return allResults;
}

// Usage examples - timeframes included in natural language
const techNews = await newsMonitoring([
  "artificial intelligence breakthroughs this week",
  "quantum computing progress recent developments", 
  "cryptocurrency regulation news today"
]);

const businessNews = await newsMonitoring([
  "Tesla earnings report Q4 2024",
  "Federal Reserve interest rate decisions this month",
  "tech layoffs news recent announcements"
]);

Error Handling

The Search API includes comprehensive error handling and validation:
const response = await valyu.search("test query", {
  maxNumResults: 5
});

if (!response.success) {
  console.error("Search failed:", response.error);
  
  // Handle specific error cases
  if (response.error?.includes("insufficient credits")) {
    console.log("Please add more credits to your account");
  } else if (response.error?.includes("invalid")) {
    console.log("Check your search parameters");
  }
  
  return;
}

// Process successful results
console.log(`Transaction ID: ${response.tx_id}`);

response.results.forEach((result, index) => {
  console.log(`${index + 1}. ${result.title}`);
  console.log(`   Relevance: ${result.relevance_score}`);
  console.log(`   Source: ${result.source}`);
  console.log(`   URL: ${result.url}`);
});

Source Types

Web Sources

  • General websites and domains
  • News sites and blogs
  • Forums and community sites
  • Documentation sites

Proprietary Sources

  • valyu/valyu-arxiv - Academic papers from arXiv
  • valyu/valyu-pubmed - Medical and life science literature
  • valyu/valyu-stocks-US - US stock market data
  • And many more. Check out the Valyu Platform Datasets for more information.