How to retrieve Medium stories for a user from the API?

Viewed 29929

I'm trying to integrate Medium blogging into an app by showing some cards with posts images and links to the original Medium publication.

From Medium API docs I can see how to retrieve publications and create posts, but it doesn't mention retrieving posts. Is retrieving posts/stories for a user currently possible using the Medium's API?

11 Answers

Edit:

It is possible to make a request to the following URL and you will get the response. Unfortunately, the response is in RSS format which would require some parsing to JSON if needed.

https://medium.com/feed/@yourhandle

⚠️ The following approach is not applicable anymore as it is behind Cloudflare's DDoS protection.

If you planning to get it from the Client-side using JavaScript or jQuery or Angular, etc. then you need to build an API gateway or web service that serves your feed. In the case of PHP, RoR, or any server-side that should not be the case.

You can get it directly in JSON format as given beneath:

https://medium.com/@yourhandle/latest?format=json    

In my case, I made a simple web service in the express app and host it over Heroku. React App hits the API exposed over Heroku and gets the data.

const MEDIUM_URL = "https://medium.com/@yourhandle/latest?format=json";

router.get("/posts", (req, res, next) => {
  request.get(MEDIUM_URL, (err, apiRes, body) => {
    if (!err && apiRes.statusCode === 200) {
      let i = body.indexOf("{");
      const data = body.substr(i);
      res.send(data);
    } else {
      res.sendStatus(500).json(err);
    }
  });
});

Nowadays this URL:

https://medium.com/@username/latest?format=json

sits behind Cloudflare's DDoS protection service so instead of consistently being served your feed in JSON format, you will usually receive instead an HTML which is suppose to render a website to complete a reCAPTCHA and leaving you with no data from an API request.

And the following:

https://medium.com/feed/@username

has a limit of the latest 10 posts.

I'd suggest this free Cloudflare Worker that I made for this purpose. It works as a facade so you don't have to worry about neither how the posts are obtained from source, reCAPTCHAs or pagination.

Full article about it.

Live example. To fetch the following items add the query param ?next= with the value of the JSON field next which the API provides.

const MdFetch = async (name) => {
  const res = await fetch(
    `https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/${name}`
  );
  return await res.json();
};

const data = await MdFetch('@chawki726');

To get your posts as JSON objects

you can replace your user name instead of @USERNAME.

https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@USERNAME

I have created a custom REST API to retrieve the stats of a given post on Medium, all you need is to send a GET request to my custom API and you will retrieve the stats as a Json abject as follows: Request :

curl https://endpoint/api/stats?story_url=THE_URL_OF_THE_MEDIUM_STORY

Response:

{
   "claps": 78,
   "comments": 1
}

The API responds within a reasonable response time (< 2 sec), you can find more about it in the following Medium article.

Related