Get Medium Posts with Username (and JavaScript!)

Andre Tai
2 min readMar 14, 2022

--

The solution can be achieved with just 14 lines of code or less.

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.

  1. 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.
  2. 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”.
  3. 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.

--

--

Andre Tai
Andre Tai

No responses yet