How to pull your blog stats from medium with C# and .NET
Since Medium’s stats are limited to 90 days and to enable deeper analysis you might want to pull the stats into your own system. Luckily there is a (hidden) http API.
Actually that API is not that hidden, it’s the same URL than the stats website:
https://medium.com/{yourHandle}/stats
The only difference is that we need to add the right Accept header to the request to return a JSON object instead of the HTML page:
Accept: application/json
The last part is authorization: Medium is using cookies for this specifically there are 2 values that need to be provided as Cookie in the request:
sid, uid
Those two values can be easily retrieved when opening the stats page in a browser and then inspecting the cookie using the browser’s developer tools:
With this information at hand we can start implementing a .NET Console Application that consumes this API.
#1 Create a HttpClient configured with the cookies, request header and base address setup:
#2 Provide a simple set of records to facilitate deserialization of the response into typed objects:
#3 As last step we need to implement a loop to query the API as it’s using paging:
And we’re already done! This should give us a list of all the posts for a given handle to use in our own apps: