Open Government Data, India

Data Access

Accessing Public APIs with JavaScript

The Open Government Data Platform India provides a range of public APIs with which developers can access data on India's economy, population, education, health, and other important topics. In this article, we'll explore the OGPL APIs and provide some basic example code in JavaScript for accessing data from these APIs.

Getting Started

To use the OGPL APIs, you'll need to register for an API key on the data.gov.in website. Once you've obtained your API key, you can start making requests to the available APIs using JavaScript.

Example API Calls

The OGPL APIs are organized into various categories, such as economy, industry, and demographics. Let's take a look at some example code for calling the Rail Transport API using JavaScript:

// API endpoint URL and parameters
const url = 'https://api.data.gov.in/resource/9a22a518-5a33-4bab-8d12-1a3c3e6f5c8e?format=json&api-key=YOUR_API_KEY&limit=all';
// Make API request using the Fetch API
fetch(url)
  .then(response => response.json())
  .then(data => {
    console.log(data);
    // Process the API response data here
  })
  .catch(error => console.error(error));

In this example, we use the fetch method of the window global object to make a GET request to the 9a22a518-5a33-4bab-8d12-1a3c3e6f5c8e API endpoint. The URL includes the format, api-key, and limit query parameters. The JSON response data is then logged to the console and can be parsed or displayed as required.

Here's another example for calling the Election Results API, this time using the Axios library:

// Import the Axios library
import axios from 'axios';
// API endpoint URL and parameters
const url = 'https://api.data.gov.in/resource/8d2daacf-937d-412f-94c0-bf9ab57e01c7?format=json&api-key=YOUR_API_KEY&limit=all';
// Make API request using Axios
axios.get(url)
  .then(response => {
    console.log(response.data);
    // Process the API response data here
  })
  .catch(error => console.error(error));

In this example, we use the axios library to make a GET request to the 8d2daacf-937d-412f-94c0-bf9ab57e01c7 API endpoint. The API response data is then logged to the console and can be parsed or displayed as required.

Conclusion

Accessing public APIs can provide a wealth of data that can be used to build applications and analyze trends. The OGPL APIs are a great resource for any developer looking to build projects with data from India. By following the examples shown here, you'll be able to start accessing the data you need in no time.

Related APIs in Data Access