Posting to Medium using Node.js and fetch()

A workaround for the unsupported Node.js Medium SDK.

You can use the Medium REST API to post new articles on Medium. They previously had a Node.js SDK to make using the API a bit simpler, but that SDK is no longer supported. Rather than relying on an unsupported SDK, you can call the REST API endpoints directly using the node-fetch package. The following code expects you to define MEDIUM_PUBLICATION_ID and MEDIUM_ACCESS_TOKEN (the Medium integration token) yourself.

const fetch = require("node-fetch");

// construct the URL to post to a publication
const MEDIUM_POST_URL = `https://api.medium.com/v1/publications/${MEDIUM_PUBLICATION_ID}/posts`;

const response = await fetch(MEDIUM_POST_URL, {
    method: "post",
    headers: {
        "Authorization": `Bearer ${MEDIUM_ACCESS_TOKEN}`,
        "Content-type": "application/json",
        "Accept": "application/json",
        "Accept-Charset": "utf-8"
    },
    body: JSON.stringify({
        title: "Title of my post",
        contentFormat: "html",
        content: "Body of my post",
        tags: ["Hello", "World"],
        publishStatus: "draft", // or "public" to immediately publish

        // optional: the canonical URL if posted elsewhere first
        canonicalUrl: "https://example.com/blog/1"
    })
});

const messageData = await response.json();

// the API frequently returns 201
if ((response.status !== 200) && (response.status !== 201)) {
    console.error(`Invalid response status ${ response.status }.`);
    throw messageData;
}

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.