Overview
This guide will walk you through creating your first application with the Axion SDK. You’ll learn how to initialize the client, fetch stock data, retrieve historical prices, and access sentiment analysis.
Initialize the Client
First, import the Axion class and create a new client instance with your API key:
import { Axion } from '@axionquant/sdk' ;
const client = new Axion ( 'your-api-key-here' );
For development and testing purposes, you can initialize the client without an API key if you’re working with a local server or endpoints that don’t require authentication.
Your First Request
Let’s fetch basic information about a stock:
const stock = await client . stocks . ticker ( 'AAPL' );
console . log ( 'Stock data:' , stock . data );
This retrieves comprehensive information about Apple Inc. (AAPL) including current price, market cap, and other key metrics.
Complete Example
Here’s a complete example that demonstrates multiple API features:
import { Axion } from '@axionquant/sdk' ;
const client = new Axion ( 'your-api-key-here' );
async function analyzeStock ( ticker ) {
try {
console . log ( `Analyzing ${ ticker } ... \n ` );
// 1. Get basic stock information
const stock = await client . stocks . ticker ( ticker );
console . log ( 'Stock Info:' , Object . keys ( stock . data ));
// 2. Get historical prices with date range
const prices = await client . stocks . prices ( ticker , {
from: '2024-01-01' ,
to: '2024-01-31' ,
frame: 'daily'
});
console . log ( `Price data points: ${ prices . data ?. length || 0 } ` );
// 3. Get company profile and information
const profile = await client . profiles . info ( ticker );
console . log ( 'Company Info:' , Object . keys ( profile . data ));
// 4. Get sentiment analysis
const sentiment = await client . sentiment . all ( ticker );
console . log ( 'Sentiment Data:' , Object . keys ( sentiment . data ));
// 5. Get recent company news
const news = await client . news . company ( ticker );
console . log ( `Recent news articles: ${ news . data ?. length || 0 } ` );
return {
stock: stock . data ,
priceCount: prices . data ?. length || 0 ,
profile: profile . data ,
sentiment: sentiment . data ,
newsCount: news . data ?. length || 0
};
} catch ( error ) {
console . error ( 'Error:' , error . message );
}
}
// Run the analysis
analyzeStock ( 'AAPL' ). then ( result => {
console . log ( ' \n Analysis complete!' );
});
Step-by-Step Walkthrough
Get Stock Tickers
First, retrieve a list of available stock tickers, optionally filtered by country and exchange: const tickers = await client . stocks . tickers ({
country: 'US' ,
exchange: 'NASDAQ'
});
console . log ( 'Available tickers:' , tickers . data . slice ( 0 , 5 ));
Fetch Stock Details
Get detailed information about a specific stock: const stock = await client . stocks . ticker ( 'AAPL' );
console . log ( 'Stock data:' , stock . data );
Retrieve Historical Prices
Fetch historical price data with custom date ranges and timeframes: const prices = await client . stocks . prices ( 'AAPL' , {
from: '2024-01-01' ,
to: '2024-01-31' ,
frame: 'daily'
});
if ( prices . data && prices . data . length > 0 ) {
console . log ( 'First data point:' , {
date: prices . data [ 0 ]. date ,
close: prices . data [ 0 ]. close
});
}
Access Company Fundamentals
Get financial metrics, earnings data, and company statistics: // Get financial metrics
const metrics = await client . financials . metrics ( 'AAPL' );
// Get earnings history
const earnings = await client . earnings . history ( 'AAPL' );
// Get company statistics
const stats = await client . profiles . statistics ( 'AAPL' );
Working with Different Asset Classes
The SDK supports multiple asset classes beyond stocks:
Cryptocurrency
ETFs
Economic Data
// Get crypto tickers
const cryptoTickers = await client . crypto . tickers ({ type: 'spot' });
// Get Bitcoin prices
const btcPrices = await client . crypto . prices ( 'BTC-USD' , {
from: '2024-01-01' ,
to: '2024-01-07' ,
frame: 'daily'
});
Error Handling
The SDK provides detailed error messages for different failure scenarios:
try {
const stock = await client . stocks . ticker ( 'AAPL' );
console . log ( 'Success!' , stock . data );
} catch ( error ) {
if ( error . message . includes ( 'HTTP Error' )) {
console . error ( 'API error:' , error . message );
} else if ( error . message . includes ( 'Connection Error' )) {
console . error ( 'Network issue:' , error . message );
} else if ( error . message . includes ( 'Authentication required' )) {
console . error ( 'Missing or invalid API key' );
} else {
console . error ( 'Unexpected error:' , error . message );
}
}
Always wrap API calls in try-catch blocks to handle potential errors gracefully. The SDK throws errors for:
HTTP errors (4xx, 5xx status codes)
Connection failures
Authentication issues
Request configuration errors
Advanced Example: Multi-Asset Analysis
Here’s a more sophisticated example that analyzes multiple data sources:
async function comprehensiveAnalysis ( ticker ) {
const client = new Axion ( 'your-api-key-here' );
try {
// Fetch data from multiple endpoints in parallel
const [ stock , prices , sentiment , financials , earnings , news ] = await Promise . all ([
client . stocks . ticker ( ticker ),
client . stocks . prices ( ticker , { from: '2024-01-01' , to: '2024-01-31' }),
client . sentiment . all ( ticker ),
client . financials . snapshot ( ticker ),
client . earnings . history ( ticker ),
client . news . company ( ticker )
]);
return {
ticker ,
currentPrice: stock . data ,
priceHistory: prices . data ,
sentiment: sentiment . data ,
financials: financials . data ,
earnings: earnings . data ,
latestNews: news . data ?. slice ( 0 , 5 ) // Get top 5 news articles
};
} catch ( error ) {
console . error ( `Analysis failed for ${ ticker } :` , error . message );
throw error ;
}
}
// Analyze multiple stocks
const tickers = [ 'AAPL' , 'MSFT' , 'GOOGL' ];
const analyses = await Promise . all (
tickers . map ( ticker => comprehensiveAnalysis ( ticker ))
);
console . log ( 'All analyses complete:' , analyses );
API Response Structure
All API methods return a response object with a data property:
const response = await client . stocks . ticker ( 'AAPL' );
// Access the data
console . log ( response . data );
// Check if data exists
if ( response . data && response . data . length > 0 ) {
// Process the data
}
The exact structure of response.data varies by endpoint. Refer to the API Reference for detailed response schemas for each endpoint.
Next Steps