If you’ve ever tried getting posts off Medium (programmatically), you’d know that Medium’s API while comprehensive, doesn’t give us an option to get a list of our own posts or someone else’s. Fortunately, there’s a workaround for this which I’ll demonstrate here. Alternatively, you can also use this package that I created to get the posts without the hassle.
- You can get a list of posts for a specific user with “https://medium.com/feed/@username” which will return a RSS feed in XML format. This isn’t particular useful for us so we’d need to convert it into a format that we’re comfortable with, in this case JSON.
- We can call an API to help us with the conversion of RSS to JSON. To do this, we’ll need to use fetch and send a request with the RSS feed’s URL from step 1 to “https://api.rss2json.com/v1/api.json?rss_url=rss_feed_url”.
- The API will return a response object which we can parse into a JSON object with response.json(). The parsed JSON object is what we need.
const fetch = require('node-fetch')const getPosts = async (username) => {
let posts = []
const feedURL = `https://medium.com/feed/@${username}`
const toolURL = `https://api.rss2json.com/v1/api.json?rss_url=${feedURL}`
await fetch(toolURL)
.then(response => response.json())
.then(data => posts = data.items())
.catch(err => console.log(err))
return posts
}module.exports = { getPosts }
And that’s it! Feel free to use my tool or ask any questions about this.