ipoalerts Logoipoalerts
Quick Start Examples

JavaScript/Node.js Examples

Quick start examples for integrating with the API using JavaScript and Node.js

This page provides practical Quick start examples for integrating with the API using JavaScript and Node.js.

Basic Request

const fetch = require('node-fetch');

async function getIPOs() {
  try {
    const response = await fetch('https://api.ipoalerts.in/ipos?status=upcoming', {
      headers: {
        'x-api-key': 'YOUR_API_KEY',
        'Content-Type': 'application/json'
      }
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const data = await response.json();
    console.log(`Found ${data.meta.count} upcoming IPOs`);
    return data.ipos;
  } catch (error) {
    console.error('Error fetching IPOs:', error);
    throw error;
  }
}

// Usage
getIPOs().then(ipos => {
  ipos.forEach(ipo => {
    console.log(`${ipo.name} (${ipo.symbol}) - ${ipo.priceRange}`);
  });
});

With Rate Limiting

class IPOAlertsClient {
  constructor(apiKey) {
    this.apiKey = apiKey;
    this.baseURL = 'https://api.ipoalerts.in';
    this.rateLimitQueue = [];
    this.isProcessing = false;
  }
  
  async makeRequest(endpoint, options = {}) {
    return new Promise((resolve, reject) => {
      this.rateLimitQueue.push({ endpoint, options, resolve, reject });
      this.processQueue();
    });
  }
  
  async processQueue() {
    if (this.isProcessing || this.rateLimitQueue.length === 0) return;
    
    this.isProcessing = true;
    
    while (this.rateLimitQueue.length > 0) {
      const { endpoint, options, resolve, reject } = this.rateLimitQueue.shift();
      
      try {
        const response = await fetch(`${this.baseURL}${endpoint}`, {
          ...options,
          headers: {
            'x-api-key': this.apiKey,
            'Content-Type': 'application/json',
            ...options.headers
          }
        });
        
        if (response.status === 429) {
          // Rate limited, wait and retry
          const retryAfter = response.headers.get('X-RateLimit-Retry-After');
          const delay = retryAfter ? parseInt(retryAfter) * 1000 : 10000;
          await new Promise(resolve => setTimeout(resolve, delay));
          this.rateLimitQueue.unshift({ endpoint, options, resolve, reject });
          continue;
        }
        
        const data = await response.json();
        resolve(data);
      } catch (error) {
        reject(error);
      }
      
      // Wait between requests to respect rate limits
      await new Promise(resolve => setTimeout(resolve, 10000)); // 10 seconds
    }
    
    this.isProcessing = false;
  }
  
  async getIPOs(filters = {}) {
    const params = new URLSearchParams(filters);
    return this.makeRequest(`/ipos?${params}`);
  }
  
  async getIPO(identifier) {
    return this.makeRequest(`/ipos/${identifier}`);
  }
}

// Usage
const client = new IPOAlertsClient('YOUR_API_KEY');

client.getIPOs({ status: 'upcoming', type: 'EQ' })
  .then(data => {
    console.log(`Found ${data.meta.count} upcoming equity IPOs`);
    return data.ipos;
  })
  .catch(error => {
    console.error('Error:', error);
  });