The awkward part of using government open data is rarely the code. It is finding the right resource, confirming that an API actually exists for it, and understanding which of the several IDs on the page belongs in your request. I have watched a five-minute experiment turn into an hour of clicking because those details were skipped.

This walkthrough starts at the current Open Government Data Platform India, commonly called OGD India or data.gov.in. By the end, you will have a personal API key, a resource endpoint, and a repeatable way to test it without pasting the key into source control.

Before creating a key, confirm that you need one

The portal contains downloadable files, catalogs, visualizations, and web APIs. A CSV download does not automatically imply that a REST endpoint exists. Open the dataset, choose its resource, and look for an API details page showing a GET /resource/{id} operation or a generated API URL.

  • API available: the resource page exposes an API operation, base URL, or generated request URL.

  • API unavailable: the page says the API does not exist and offers Request API. Generating a key will not create the missing endpoint.

  • File download only: use the published CSV, XLS, JSON, or XML file unless the publisher later enables an API.

Create the account and API key

The portal’s sign-in experience has changed since the original version of this article was published. Current help material points users to the Login control and includes JanParichay registration guidance. Labels may move again, so follow the account concepts below rather than relying on an old screenshot pixel for pixel.

  1. Open data.gov.in and select Login in the page header.

  2. Sign in with the identity options presented by the portal. If you do not have an account, complete the linked JanParichay registration and verification flow.

  3. Return to data.gov.in after authentication and open the user or My Account area.

  4. Find the API-key management option, usually labelled Generate API Key, Generate Your New API Key, or similar.

  5. Generate the key once, copy it to a password manager or secrets store, and avoid sharing screenshots that reveal it.

Read the generated API URL instead of rebuilding it blindly

On an API-enabled resource page, the portal provides the operation and resource identifier. A common public-data request follows this shape:

request-url.txttext
https://api.data.gov.in/resource/RESOURCE_ID
  ?api-key=YOUR_API_KEY
  &format=json
  &offset=0
  &limit=10

Generic data.gov.in resource URL; replace both uppercase placeholders.

What each piece contributes

  • RESOURCE_ID identifies the selected dataset resource and normally appears as a UUID on its API page.

  • api-key carries your personal access key. The parameter name includes a hyphen.

  • format=json requests JSON when that format is supported by the endpoint.

  • offset=0 starts at the first record, while limit=10 keeps the initial response small enough to inspect.

Make a careful first request with curl

Keeping the key in an environment variable is a modest improvement over putting it directly in shell history. Set it for the current terminal session, substitute the real resource UUID, and let curl encode each query parameter.

Terminalbash
read -rsp "data.gov.in API key: " DATA_GOV_API_KEY && printf '\n'
RESOURCE_ID='replace-with-resource-uuid'

curl --fail-with-body --silent --show-error --get \
  "https://api.data.gov.in/resource/$RESOURCE_ID" \
  --data-urlencode "api-key=$DATA_GOV_API_KEY" \
  --data-urlencode 'format=json' \
  --data-urlencode 'offset=0' \
  --data-urlencode 'limit=10'
{
  "index_name": "…",
  "title": "…",
  "total": 123,
  "count": 10,
  "limit": "10",
  "offset": "0",
  "records": [ … ]
}

Details hiding in that command

  • read -s suppresses terminal echo while the key is entered; -p prints the prompt and -r prevents backslash processing.

  • curl --get keeps this as an HTTP GET request and appends the values supplied by --data-urlencode to the query string.

  • --fail-with-body returns a failing exit status for HTTP errors while preserving the server response that may explain the problem.

  • The illustrated metadata keys are common, but the record fields and even some envelope details vary by resource. Inspect the real response instead of hard-coding the sample.

Call the same endpoint from Python

For a script or backend job, pass parameters separately and add a timeout. That produces clearer code than concatenating a long URL and makes encoding the values the HTTP library’s responsibility.

fetch_open_data.pypython
import os
import requests
 
RESOURCE_ID = "replace-with-resource-uuid"
API_URL = f"https://api.data.gov.in/resource/{RESOURCE_ID}"
 
api_key = os.environ.get("DATA_GOV_API_KEY")
if not api_key:
    raise SystemExit("Set DATA_GOV_API_KEY before running this script")
 
response = requests.get(
    API_URL,
    params={
        "api-key": api_key,
        "format": "json",
        "offset": 0,
        "limit": 10,
    },
    timeout=30,
)
response.raise_for_status()
 
payload = response.json()
print(f"Returned records: {len(payload.get('records', []))}")
for record in payload.get("records", []):
    print(record)

A small Python client using an environment variable and explicit request timeout.

Why this client is safer to extend

  • os.environ.get reads the credential at runtime, keeping it out of the Python file and repository.

  • requests.get receives query parameters through params, and timeout=30 prevents an unbounded network wait.

  • raise_for_status() turns HTTP 4xx and 5xx responses into exceptions before the code assumes valid JSON.

  • payload.get("records", []) tolerates a missing records array, but production code should log and inspect unexpected response schemas.

When the first call does not work

  • The page says “API does not exist”: use the portal’s Request API action or consume the downloadable resource. A valid key cannot activate an unpublished endpoint.

  • Authentication or forbidden response: verify that the key is complete, has no surrounding spaces, and belongs to the account currently in use. Regenerate it if exposure is suspected.

  • Not found: copy the resource UUID again. A catalog UUID, truncated UUID, or resource from a different state portal may lead to another path.

  • An empty `records` array: remove optional filters, reset offset to 0, and confirm that the resource actually contains published rows.

  • HTML arrives instead of JSON: make sure the request targets api.data.gov.in, not the human-facing resource page, and include format=json where the endpoint supports it.

  • A browser app would expose the key: proxy the request through your backend and apply rate limiting, origin checks, logging, and secret rotation there.

Pagination and responsible use

Start with a small limit, read the returned count and total metadata when present, and advance offset deliberately. Do not assume every resource accepts the same maximum page size or filter syntax; the API details page is the contract for that resource.

  • Cache data when its publication frequency permits instead of downloading the same rows repeatedly.

  • Record the resource ID and retrieval timestamp so later analysis is reproducible.

  • Expect schemas and field names to evolve when a publishing department revises a dataset.

  • Review the dataset’s metadata, access type, update frequency, and Government Open Data License information before redistribution.

Useful official pages