DEV Community

Cover image for How to Publish an Article to Medium Using Python and the Medium API
Luca Liu
Luca Liu

Posted on

How to Publish an Article to Medium Using Python and the Medium API

Introduction

As someone who uses Obsidian to write articles, I often find myself needing to copy and format my content manually when publishing to Medium. This process can be time-consuming and repetitive, especially when dealing with Markdown files. To streamline my workflow, I decided to develop a Python script that automates the publication of Markdown files directly to Medium. In this article, I’m excited to share with you how to programmatically publish articles using the Medium API, making the process faster and more efficient.

Setting Up the Medium API

To interact with Medium’s API, you first need to generate an integration token. This token will allow your Python script to authenticate and perform actions on your behalf.

Steps to Generate an Integration Token:

  1. Go to your Medium Security and apps.
  2. Scroll down to the “Integration tokens” section.
  3. Click on “Get integration token.”
  4. Copy the generated token and keep it safe; you’ll need it for your script.

With the token in hand, you’re ready to start coding.

Getting user’s details and publications

Here’s the Python code you’ll be using to interact with the Medium API:

import requests  

# Replace these with your actual Medium integration token and file path  
MEDIUM_TOKEN = 'your_medium_integration_token'

headers = {  
    'Authorization': f'Bearer {MEDIUM_TOKEN}',  
    'Content-Type': 'application/json',  
    'Accept': 'application/json',  
    'host': 'api.medium.com',  
    'Accept-Charset': 'utf-8'  
}  
url = '''https://api.medium.com/v1/me'''  
response = requests.get(url=url, headers=headers)  

print('status_code is: ',response.status_code)  
print('response text:', response.json())  
print('userId:', response.json()['data']['id'])
Enter fullscreen mode Exit fullscreen mode

Fetching User Information
When you run the script, it sends a request to Medium’s API to fetch your user information. The response includes details like your user ID, which is required to publish content.

Publishing an Article

Now that you’ve successfully retrieved your user ID from the Medium API, you can move on to publishing an article. The process involves sending a POST request to Medium’s API with the article content and some metadata.

import requests
import json

# Replace with your actual Medium integration token and user ID
MEDIUM_TOKEN = 'your_medium_integration_token'
USER_ID = 'your_user_id'

headers = {
    'Authorization': f'Bearer {MEDIUM_TOKEN}',
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'host': 'api.medium.com',
    'Accept-Charset': 'utf-8'
}

url = f'https://api.medium.com/v1/users/{USER_ID}/posts'

# Article content and metadata
data = {
    "title": "Your Article Title",
    "contentFormat": "markdown",  # Choose 'html', 'markdown', or 'plain'
    "content": "# Hello World!\nThis is my first article using the Medium API.",
    "tags": ["python", "api", "medium"],
    "publishStatus": "draft"  # Choose 'public' or 'draft'
}

# Sending the POST request
response = requests.post(url=url, headers=headers, data=json.dumps(data))

print('Status code:', response.status_code)
print('Response:', response.json())
Enter fullscreen mode Exit fullscreen mode

Now you can head over to Medium to check your latest draft. Once you’ve confirmed that everything is formatted correctly, you can go ahead and publish it directly!


Explore more

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

🚀 Connect with me on LinkedIn

Top comments (2)

Collapse
 
devasservice profile image
Developer Service

I am confused.

The documentation says that the APi is no longer support and that you cannot request new tokens…

github.com/Medium/medium-api-docs

So I can you make this work?

Collapse
 
luca1iu profile image
Luca Liu

Hi I tested the '2. Authentication' part, it doesn't work, but the '3. Resources' part works.