Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vipickering/f74e4875249ac6ddd7b3bb63a287d50e to your computer and use it in GitHub Desktop.
Save vipickering/f74e4875249ac6ddd7b3bb63a287d50e to your computer and use it in GitHub Desktop.
var mediumIntegrationToken = "REPLACE_WITH_YOUR_MEDIUM_INTEGRATION_TOKEN";
// That's it. No need to edit anything else below.
// split the comma-separated tags string into an array then remove any whitespace
var tagsArray = input.medium_tags.split(',');
for (var i = 0; i < tagsArray.length; i++) {
tagsArray[i] = tagsArray[i].trim();
}
var data = JSON.stringify({
"title": input.medium_title,
"contentFormat": "html",
"content": input.medium_content,
"canonicalUrl": input.medium_url,
"publishStatus": input.medium_publish_status,
"tags": tagsArray
});
// Look up the author id
fetch('https://api.medium.com/v1/me', {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + mediumIntegrationToken
}
})
.then(function(res) {
return res.json();
})
.then(function(body) {
return body.data.id;
})
.then(function(authorId) {
// Now create the medium post
var apiUrl = 'https://api.medium.com/v1/users/' + authorId + '/posts';
fetch(apiUrl, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + mediumIntegrationToken
}
})
.then(function(res) {
return res.json();
})
.then(function(body) {
var output = body.data;
callback(null, output);
})
.catch(callback);
});
@vipickering
Copy link
Author

Updated to allow sending to the drafts folder via publishStatus.

  • The data can some times become mangled (if you embed an SVG for example).
  • The data is also missing a feature image (there is no way to send this, unless you have special permission).
  • The data is missing a subtitle and description that need to be manually added.

All simple and quick to do before publishing so pushing to drafts may be preferable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment