JavaScript/TypeScript SDK

Use FalakAPI in your JavaScript or TypeScript applications with these examples and best practices.

Installation

FalakAPI is a REST API, so you can use it with any HTTP client. Here are examples using native fetch:

Basic Usage

// Get prayer times
async function getPrayerTimes(lat, lng, date) {
  const response = await fetch(
    `https://api.falakapi.com/v1/prayer?lat=${lat}&lng=${lng}&date=${date}`,
    {
      headers: {
        'X-API-Key': 'fak_your_api_key_here'
      }
    }
  );
  
  if (!response.ok) {
    throw new Error(`API error: ${response.status}`);
  }
  
  return await response.json();
}

// Usage
getPrayerTimes(28.66, 77.07, '2025-01-15')
  .then(data => console.log(data))
  .catch(error => console.error(error));

Helper Function

class FalakAPI {
  constructor(apiKey) {
    this.apiKey = apiKey;
    this.baseURL = 'https://api.falakapi.com/v1';
  }

  async request(endpoint, params = {}) {
    const queryString = new URLSearchParams(params).toString();
    const url = `${this.baseURL}${endpoint}?${queryString}`;
    
    const response = await fetch(url, {
      headers: {
        'X-API-Key': this.apiKey
      }
    });
    
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error || 'API request failed');
    }
    
    return await response.json();
  }

  async getPrayerTimes(lat, lng, date) {
    return this.request('/prayer', { lat, lng, date });
  }

  async getHijriDate(date) {
    return this.request('/hijri', { date });
  }

  async getQibla(lat, lng) {
    return this.request('/qibla', { lat, lng });
  }

  async getMoonPhase(lat, lng, date) {
    return this.request('/moon', { lat, lng, date });
  }
}

// Usage
const api = new FalakAPI('fak_your_api_key_here');
const prayers = await api.getPrayerTimes(28.66, 77.07, '2025-01-15');
console.log(prayers);

💡 Tip

Store your API key in environment variables (e.g., process.env.FALAKAPI_KEY) and never commit it to version control.