PROGRAMMING & APIs

Unofficial Medium API: Get Posts Using Python

With "requests" library ONLY!

Image by the Author

Hey guys!

Today, we'll be fetching posts (articles or stories) written by a user from Medium Platform (https://medium.com), using Python and Medium API (Unofficial Version). It's pretty straightforward and simple ...

So without wasting much time, let's get to the code!


For a more advanced tutorial, using medium-api python package, go to -


Prerequisite -

  1. Install python
  2. Install requests library (pip install requests)
  3. Register to RapidAPI to get YOUR_APIKEY (It won't take long, trust me!)


Code in Steps -

  1. Import requests library
import requests

2. Set headers and base_url

headers = {
  "x-rapidapi-host": "YOUR_APIHOST", # 'medium2.p.rapidapi.com'
  "x-rapidapi-key": "YOUR_APIKEY"
}
base_url = 'https://medium2.p.rapidapi.com'

3. Set username

username = 'nishu-jain'

4. Get user_id

response = requests.get(
                    base_url + '/user/id_for/' + username, 
                    headers = headers
)
 
json_data = response.json()
user_id = json_data['id']

5. Get all the articles (article_ids) written by the author

response = requests.get(
                    base_url + '/user/' + user_id + '/articles',         
                    headers = headers
)
 
json_data = response.json()
article_ids = json_data['associated_articles']

6. Get information related to each article

articles = []
 
for article_id in article_ids:
   response = requests.get(base_url + '/article/' + article_id)
   article = response.json()
   print('[FETCHING]: ', article['title'])
   articles.append(article)

7. Done! You've successfully got all the posts using python

print('Total number of articles: ', len(articles), '\n')
print('Details of the first article: \n')
print(articles[0])

Output of the first article's info -

{ 
 'id': '562c5821b5f0',
 'title': 'About Me :) - Nishu Jain',
 'subtitle': 'Who am I and what do I do ...',
 'author': '1985b61817c3',
 'tags': ['about-me', 'nishu-jain', 'bio', 'software-engineer', 'introduction'],
 'publication_id': '*Self-Published*',
 'published_at': '2021-04-17 17:42:10',
 'last_modified_at': '2021-09-13 06:48:43',
 'claps': 52,
 'voters': 2,
 'word_count': 527,
 'reading_time': 2.3720125786164,
 'topics': ['self'],
 'url': 'https://nishu-jain.medium.com/about-me-nishu-jain-562c5821b5f0',
 'image_url': 'https://miro.medium.com/1*4cUFmh4kDyGvf4y-73tBaA.png',
}

Full Code -

Help & References -

In case you're stuck somewhere, read the following articles for more information -

Or you can contact me, I would be happy to help ...

nishu@mediumapi.com

Thanks for reading and have a nice day!


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