NPR One
NewsExploring the NPR API with JavaScript
If you're looking for a way to access news articles, topics, and programs from National Public Radio (NPR), you're in luck. NPR offers a public API that allows developers to retrieve data from its extensive database of news content.
In this guide, we'll explore how to use the NPR API with JavaScript, and we'll provide some examples of API calls that you can try out for yourself.
Getting Started with the NPR API
To begin using the NPR API, you'll need to sign up for an API key. This key is what you'll use to authenticate your requests and access the API's resources.
Once you have your API key, you can start making API requests using a GET request to a specific endpoint. Endpoints are URLs that correspond to different types of data within the NPR API.
Example API Calls
Here are a few examples of API calls you can make using JavaScript and the NPR API.
Retrieve a List of News Articles
const apiKey = "YOUR_API_KEY_HERE";
const nprEndpoint = "https://api.npr.org/query";
const query = "term=tech";
const apiUrl = `${nprEndpoint}?${query}&apiKey=${apiKey}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
This code sends a GET request to the NPR API with a search term of "tech". The API responds with a list of news articles that match the search term.
Retrieve Information on a Specific Topic
const apiKey = "YOUR_API_KEY_HERE";
const nprEndpoint = "https://api.npr.org/query";
const id = "1007";
const query = `id=${id}`;
const apiUrl = `${nprEndpoint}?${query}&apiKey=${apiKey}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
This code sends a GET request to the NPR API with a specific topic ID of "1007". The API responds with information on the topic, including its name, description, and related news articles.
Retrieve Information on a Specific Program
const apiKey = "YOUR_API_KEY_HERE";
const nprEndpoint = "https://api.npr.org/query";
const id = "2";
const query = `id=${id}`;
const apiUrl = `${nprEndpoint}?${query}&apiKey=${apiKey}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
This code sends a GET request to the NPR API with a specific program ID of "2". The API responds with information on the program, including its name, description, and related news articles.
Conclusion
The NPR API offers a wealth of information for developers looking to access news content from National Public Radio. With the right API key and a little bit of JavaScript knowledge, you can build your own applications that consume data from this robust API endpoint.