A guide to extracting data from Medium API
Does Medium have an API? This is a question that has been asked several times by users. The answer is yes. Medium's API is a JSON-based OAuth2 API. All requests are made to endpoints beginning: https://api.medium.com/v1 and all requests must be secure, i.e. https, not http.
We can interact with the Medium Api using the programming language of our choice solely to get the data we are interested in. In this post, we will use Python language to get our data. Here is the list of the data we would be getting.
- List of publications by user
- Statistical analysis of Medium account
First Things First: Integration Token
The first thing we need to do before we can start pulling data from Medium is to acquire an integration token. We can do this by going to settings under our profile tab and then click on integration token. Click on "get integration token" after passing a description for the token. This brings out a long list of hex strings that we can use as access tokens in our code.
We can also use the Developer option right below the integration token whereby we can create a new application here and then a client id and client secret will be given to us. We can as well use these ids' in our code to generate our access token.
1. Go to Settings under your Medium profile tab
2. Click on Integration Token and generate one
3. Alternatively, use the Developer option to create an app and get a client_id and client_secret
Access Token
We will use the medium token Url endpoint method along with our credentials to request for an access token which we can pass to the user publication endpoint to get our data.
Refresh Token
We will generate a new access token using the refresh token which has a longer time than the earlier access token generated. By doing this, we will not keep running into error showing "access token has expired"
Get User Publications
We can get our user publications with the function below by passing our headers which have the access token. With the request library, we will send a get request which will return the data we need. Afterward, we will clean this data.
Medium data are usually in RSS Format.
Get Statistical Data
We can get statistical data for a medium account including both audience and story stats. This data contains monthly growth, number of followers, viewers, readers, fans, and so on.
We'll be making use of the medium stats Library developed on top Pypi. This library has the command line usage but we'll be making use of the python approach.
Steps to follow
- Sign in to Medium account
- Go to the browser's developer tools (either Chrome or any browser) and find the tab which holds cookies: (in chrome, go to Settings → privacy & security → cookies & other sites data → see all cookies & sites data)
- Scroll through the cookies until you find medium and copy the
uidandsid.
These two things will be used with the library inside our code to generate our data. We will then proceed to our code.
We will define the function to parse all our parameters
Data Cleaning
With the various data we saved in a list, we can demonstrate with the event logs data by cleaning it and see what the data looks like.
Let's open an empty dictionary here and loop through the events logs and parse an event key inside
We will calculate the length of the events using the earlier dictionary we created.
Now, we can create a different empty list to which we can append all data coming from our code
We will loop through the data using the size of the data to get all the data we need and then append each to its related opened empty list.
Finally, we will use Pandas to convert these lists we created and combine them all to one DataFrame.
Conclusion
We have come to the end of this tutorial. I hope you will follow the steps above and go ahead to get data from Medium API successfully.
Let CNDRO build the data infrastructure your business needs
From API integrations to analytics pipelines — we engineer the backend so you can focus on growth.
Book a free consultation →