JAVASCRIPT PROGRAMMING

How To Get Articles Written By a Medium User Using JavaScript

Unofficial Medium API + Fetch library = 🚀

Image by the Author

Are you tired of manually collecting data from the web? Do you want to quickly and easily access the articles written by a specific Medium user, but don't know where to start?

Look no further, because in this article we will show you how to scrape articles written by a Medium user using JavaScript.

With just a few lines of code, you can extract the content you need from the Medium platform and use it for your own purposes. We will explain the steps involved and provide sample code to help you get started. So if you want to save time and effort when collecting data from the web, keep reading.

Today, we'll code a simple program that takes Medium username as the input and outputs a list of article titles written by the user. We'll use JavaScript as our programming language, so you can run the program using NodeJS on your local machine.

Use the following command to execute the JS file:

node get_user_articles.js

Also, here we will use Unofficial Medium API for the data extraction, so be ready with your API key.

💡 For getting your API Key, please subscribe to our Unofficial Medium API on RapidAPI Platform. Here, you'll get sufficient amount of calls per month for free to get started.

Now let's jump to the code!


Code -

/* get_user_articles.js */
 
// Set the API key, base URL, and username
const apiKey = "";
const baseUrl = "https://medium2.p.rapidapi.com/";
const username = "";
 
// Set the headers for the request
const headers = {
  "x-rapidapi-key": apiKey,
  "x-rapidapi-host": "medium2.p.rapidapi.com"
};
 
// Function to get the user articles for a username
function getUserId(username) {
  // Set the endpoint and parameters for the request
  const endpoint = `user/id_for/${username}`;
 
  // Send the request and return the user ID
  return fetch(baseUrl + endpoint, { headers })
    .then(response => response.json())
    .then(data => data.id);
}
 
// Function to get the user articles for a user ID~
function getUserArticlesIds(userId) {
  // Set the endpoint and parameters for the request
  const endpoint = `user/${userId}/articles`;
 
  // Send the request and return the user articles
  return fetch(baseUrl + endpoint, { headers })
    .then(response => response.json())
    .then(data => data.associated_articles);
}
 
// Function to get the article information for a list of article IDs
async function getArticlesInfo(articleIds) {
  // Set the endpoint and parameters for the request
  const endpoint = `article/`;
 
  // Create an array of promises for each article ID
  const promises = articleIds.map(articleId =>
    fetch(baseUrl + endpoint + articleId, { headers }).then(response => response.json())
  );
 
  // Wait for all of the promises to resolve and return the array of articles
  return Promise.all(promises)
}
 
 
// Get the user ID for the given username
getUserId(username)
  // Get the user articles for the user ID
  .then(userId => getUserArticlesIds(userId))
  // Get the articles information
  .then(articlesIds => getArticlesInfo(articlesIds))
  // Print the articles titles;
  .then(articles_info => { 
    articles_info.map(article_info => console.log(article_info.title)) })

Code Explanation

The code is using the Unofficial Medium API to get articles information for a given username. It sets the API key and base URL for the API and defines a headers object that will be used for making requests to the API.

It defines three functions:

The code then uses these functions to get the user's articles and print the titles of the articles to the console.


Conclusion -

Scraping articles written by a Medium user using JavaScript is a simple and effective way to quickly collect data from the web. By following the steps outlined in this article and using the provided sample code, you can easily extract the content you need from the Medium platform.

This can save you time and effort, and allow you to focus on using the data for your own purposes. Whether you are a researcher, a marketer, or just someone who is curious about the content on Medium, scraping can be a useful tool to help you access the information you need.

So why not give it a try and see how it can benefit you?


Important Resources 💡


PS: We post tutorials, videos, code snippets, and insights related to Medium and Unofficial Medium API. So be sure to follow us on