Using the Chronicling America API in JavaScript

If you're looking to explore the vast collection of historical newspapers on Chronicling America, you can do so using their public API. In this blog post, we'll demonstrate how to use the Chronicling America API in JavaScript, with example code snippets.

Getting Started

Before you can start making API requests, you'll need to register for an API key. Head to the Chronicling America API registration page to get started.

Once you have your API key, you're ready to start making requests.

Example 1: Searching for Newspapers

Let's say you want to search for all newspapers in the Chronicling America collection that mention the word "suffrage". Here's how you can do that in JavaScript:

const apiKey = 'YOUR_API_KEY';
const searchTerm = 'suffrage';
const url = `https://chroniclingamerica.loc.gov/search/titles/results/?terms=${searchTerm}&format=json&api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, we're using the fetch method to make a GET request to the Chronicling America API. We pass in the API key and the search term as query parameters in the URL, and set the response format to JSON. We then parse the response using the .json() method, and log the resulting data to the console.

Example 2: Retrieving Newspaper Pages

Let's say you want to retrieve a specific newspaper page from the Chronicling America collection. Here's an example code snippet that shows you how to do that in JavaScript:

const apiKey = 'YOUR_API_KEY';
const pageUrl = 'https://chroniclingamerica.loc.gov/lccn/sn83045462/1910-01-10/ed-1/seq-1.json';
const url = `${pageUrl}?format=json&api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, we're using the fetch method again to make a GET request to the Chronicling America API. We pass in the API key and the URL of the specific newspaper page we want to retrieve as query parameters in the URL, and set the response format to JSON. We then parse the response using the .json() method, and log the resulting data to the console.

Example 3: Retrieving OCR Text

Finally, let's say you want to retrieve the OCR text for a specific newspaper page from the Chronicling America collection. Here's an example JavaScript code snippet that shows you how to do that:

const apiKey = 'YOUR_API_KEY';
const pageUrl = 'https://chroniclingamerica.loc.gov/lccn/sn83045462/1910-01-10/ed-1/seq-1.json';
const ocrUrl = `${pageUrl}/ocr.json`;
const url = `${ocrUrl}?api_key=${apiKey}`;

fetch(url)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, we're once again using the fetch method to make a GET request to the Chronicling America API. We pass in the API key and the OCR URL of the specific newspaper page we want to retrieve as query parameters in the URL. We then parse the response using the .json() method, and log the resulting data to the console.

Wrapping Up

In this blog post, we've shown you how to use the Chronicling America API in JavaScript, with example code snippets for newspaper search, newspaper page retrieval, and OCR text retrieval. Hopefully, this has given you a good starting point for exploring the vast collection of historical newspapers on Chronicling America using their public API.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf