This folder contains practical examples demonstrating how to use the Perigon TypeScript SDK to access news data, stories, and AI-powered features.
-
Get a Perigon API Key
- Sign up at perigon.io
- Get your API key from the dashboard
-
Set Environment Variable
export PERIGON_API_KEY="your_api_key_here"
Or create a
.envfile in this directory:PERIGON_API_KEY=your_api_key_here
-
Install Dependencies
cd examples npm install
Run with: npm run basic
Demonstrates fundamental SDK features:
- β Basic article search with queries
- β Filtering by source and date range
- β Story search (clustered articles)
- β Company-focused article search
- β Proper error handling
What you'll learn:
- How to initialize the SDK with your API key
- Basic search parameters and filtering
- Handling different response types
- Error management and troubleshooting
Run with: node advanced-usage.js
Shows sophisticated features:
- π€ AI-powered article summarization
- π’ Company and entity search
- π Sentiment analysis filtering
- π Geographic news filtering
- π Topic-based story discovery
- π Complex multi-parameter searches
- π οΈ Custom middleware for logging
What you'll learn:
- Advanced filtering techniques
- AI summarization capabilities
- Sentiment-based content analysis
- Geographic and topical filtering
- Request/response middleware
- Complex parameter combinations
// Basic search
const articles = await perigon.searchArticles({
q: "artificial intelligence",
size: 10,
sortBy: "date",
});
// Advanced filtering
const filtered = await perigon.searchArticles({
q: "technology OR startup",
source: ["reuters.com", "bloomberg.com"],
category: ["Tech", "Business"],
from: new Date("2024-01-01"),
excludeLabel: ["Opinion"],
size: 5,
});// Find trending stories
const stories = await perigon.searchStories({
q: "climate change",
minUniqueSources: 5,
sortBy: "updatedAt",
size: 3,
});// Generate summaries
const summary = await perigon.searchSummarizer({
summaryBody: {
summaryType: "keyPoints",
language: "en",
},
q: "cryptocurrency",
size: 10,
});// Find companies
const companies = await perigon.searchCompanies({
name: "Microsoft",
size: 5,
});import { V1Api, Configuration } from "@goperigon/perigon-ts";
const configuration = new Configuration({
apiKey: () => Promise.resolve(process.env.PERIGON_API_KEY),
basePath: "https://api.perigon.io", // Optional: custom endpoint
middleware: [
/* custom middleware */
], // Optional: request/response middleware
});
const perigon = new V1Api(configuration);| Parameter | Description | Example |
|---|---|---|
q |
Search query with Boolean operators | "AI AND (machine learning OR deep learning)" |
size |
Number of results (max 100) | 10 |
sortBy |
Sort order | 'date', 'relevance', 'addDate' |
from / to |
Date range filtering | new Date('2024-01-01') |
source |
Filter by news sources | ['nytimes.com', 'reuters.com'] |
category |
Content categories | ['Tech', 'Business', 'Politics'] |
sourceCountry |
Geographic filtering | ['us', 'gb', 'ca'] |
- AND:
crypto AND bitcoin - OR:
tesla OR "electric vehicles" - NOT:
apple NOT fruit - Exact phrases:
"artificial intelligence" - Wildcards:
technolog*(matches technology, technological, etc.)
// Monitor mentions of your company
const mentions = await perigon.searchArticles({
companyName: "YourCompany",
sortBy: "date",
size: 20,
});// Research industry trends
const trends = await perigon.searchStories({
q: 'fintech OR "financial technology"',
category: ["Business", "Tech"],
minUniqueSources: 3,
});// Analyze sentiment around topics
const sentiment = await perigon.searchArticles({
q: "renewable energy",
positiveSentimentFrom: 0.6,
size: 10,
});// Track competitor coverage
const competitor = await perigon.searchArticles({
companyName: "CompetitorName",
excludeLabel: ["Press Release"],
from: lastWeek,
});The SDK provides specific error types for different scenarios:
try {
const result = await perigon.searchArticles({ q: "test" });
} catch (error) {
if (error.name === "ResponseError") {
// HTTP errors (401, 403, 429, etc.)
console.log(`HTTP ${error.response.status}: ${error.response.statusText}`);
} else if (error.name === "RequiredError") {
// Missing required parameters
console.log(`Missing parameter: ${error.field}`);
} else if (error.name === "FetchError") {
// Network/connection errors
console.log(`Network error: ${error.message}`);
}
}- Verify your API key is correct
- Check if the key is properly set in environment variables
- Ensure your API key hasn't expired
- Reduce request frequency
- Implement exponential backoff
- Check your subscription plan limits
- Verify parameter names and values
- Check date formats (ISO 8601)
- Ensure array parameters are properly formatted
- API Documentation: docs.perigon.io
- SDK Repository: github.com/goperigon/perigon-ts
- Support: Contact support through the Perigon dashboard
- Community: Join discussions on GitHub Issues
# Basic example
npm run basic
# Advanced example
node advanced-usage.js
# Setup dependencies
npm run setup
# Get help
npm run help- Copy an existing example file
- Modify the search parameters and logic
- Add your custom functionality
- Test with your API key
// Request logging middleware
const loggingMiddleware = {
pre: async (context) => {
console.log(`Request: ${context.url}`);
},
post: async (context) => {
console.log(`Response: ${context.response.status}`);
},
};
// Error recovery middleware
const retryMiddleware = {
onError: async (context) => {
if (context.error.name === "FetchError") {
// Return fallback response
return new Response(JSON.stringify({ articles: [] }));
}
},
};Happy coding with Perigon! π
For questions or issues, please check the documentation or create an issue in the GitHub repository.