mta-js uses an API key from mtaapi.dev — the hosted version of this SDK — to authenticate every data request. You pass the key once when you create theDocumentation Index
Fetch the complete documentation index at: https://mtaapi.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
MTA client, and the SDK handles the rest automatically. This page explains how to get a key, store it safely, and troubleshoot authentication failures.
Get your API key
API keys are free and issued through mtaapi.dev. Using the hosted API is the fastest and easiest way to use mta-js — no MTA developer registration, no BusTime key, no GTFS imports.- Go to mtaapi.dev and sign up for a free account.
- Copy your API key from the dashboard.
Prefer to self-host and call MTA feeds directly? See Direct MTA feeds on the Quick Start page. The rest of this page assumes you’re using the hosted mtaapi.dev API.
Configure your environment
Store your API key as an environment variable so it stays out of your source code. Add the following line to a.env file at the root of your project:
.env
.env is listed in your .gitignore:
.gitignore
Initialize the client
Pass your API key to theMTA constructor as apiKey. With an apiKey set, requests are routed to https://www.mtaapi.dev/api/v1 automatically.
Authentication errors
If your API key is missing, incorrect, or expired, the API returns an HTTP error and mta-js surfaces it as a thrown error.| Status code | Meaning |
|---|---|
401 Unauthorized | The API key was not provided or is malformed. Check that MTA_API_KEY is set in your environment and that you’re passing it correctly to the constructor. |
403 Forbidden | The API key is recognized but not authorized to access the requested resource. Your key may have expired or been revoked. |
- Confirm the environment variable is set by logging
process.env.MTA_API_KEYbefore initializing the client. It should not beundefined. - Double-check that the key you copied from mtaapi.dev does not have leading or trailing whitespace.
- If you recently regenerated your key, update it in every environment where it’s used (local
.env, hosting platform secrets, CI/CD variables). - If the problem persists, log in to mtaapi.dev to verify your key is still active.