How to Get Archived Medium Articles

(Using Medium API Python Package)

In this guide, we'll show you how to retrieve archived articles from Medium for a specific tag, using the medium-api library in Python. The example fetches archived articles tagged with "artificial intelligence" from December 2023. Let's walk through the code step by step to understand how this works.

Prerequisite

Before proceeding, make sure you install the required package by running:

pip install medium-api

Once installed, follow the steps below to retrieve archived articles from Medium.

Step 1: Import Necessary Libraries

We begin by importing the required libraries. In this case, we need the os library to access environment variables and the Medium class from the medium-api package to interact with Medium’s API.

Step 2: Get Your API Key

To use Medium's API, you need an API key. In this code, the API key is retrieved from an environment variable called RAPIDAPI_KEY. Make sure you've set this environment variable in your system before running the script.

You can set the environment variable in your terminal like this:

export RAPIDAPI_KEY='your_api_key_here'

Alternatively, you can replace os.getenv('RAPIDAPI_KEY') with your actual API key string directly in the code (though using environment variables is more secure).

Step 3: Create a Medium Object

Next, we create an instance of the Medium object, passing the API key as an argument. This object will allow us to interact with Medium's API.

Step 4: Fetch Archived Articles by Tag

We then fetch archived articles for the specified tag. In this example, we are retrieving up to 30 articles tagged with “artificial-intelligence” from December 2023.

Step 5: Fetch and Display Article Titles

After fetching the archived articles, we call the fetch_articles() method to load them into the archived_articles object. Finally, we iterate through the articles and print their titles.

This loop will display the titles of all the articles retrieved for the given tag and date range.

Conclusion

This simple script demonstrates how to fetch archived articles from Medium based on specific tags and dates using the medium-api library. Make sure you have the API key properly set up and adjust the tag, year, and month parameters as per your needs.

By following these steps, you’ll be able to automate the process of retrieving Medium articles for analysis, archiving, or personal use.

Output

The following is an example of the output you'll see when running the script: