mastodon api python featured

How To Create a Post With Mastodon API Using Python

In this post, we’re going to make a simple Python algorithm for creating a post with Mastodon API. Furthermore, we’re going to use Mastodon official server to register an app with which we’ll be able to access API.

Mastodon is a decentralized open-source social network platform, which allows you to host your own server. Furthermore, its features are similar to Twitter, such as its user interface and allowing user to post short posts – 500 characters long.

Setting up new application

In order to gain access to Mastodon API functionalities, we need to register a new application on the server. Because we’re going to run this app on the official server, we can register it through the Mastodons user interface.

We can do so by clicking on the Preferences tab and then going to the Development section.

Creating new application for using Mastodon API

Here, you can go on ahead and click the New application button, and input all necessary information and submit. By default, scopes for reading, writing and following will already be selected.

Once you’ve created your application, you’ll be able to see on the very top of its settings its client key, client secret, and your access token. This access token is what we’re going to need when we’ll be calling Mastodon API through our Python code.

Creating posts with Mastodon API using Python

First of all, we’ll need to install and import all the necessary libraries. In case you’ve never worked with Mastodon library before, here is the pip command to install it.

pip install Mastodon.py

And here are all the imports we’ll need.

import os
import json
from mastodon import Mastodon

Next, we’ll define a function that will retrieve our access token from a separate json file. Reason for this is so we don’t expose our access token directly in our python script, in case you’ll want to share it with others.

In this case, I saved my token into a json file inside the project directory.

ROOT = os.path.dirname(__file__)

def get_token(token_name):
    with open(os.path.join(ROOT, 'auth.json'), 'r') as auth_file:
        auth_data = json.load(auth_file)
        token = auth_data[token_name]
        return token

Now, we need to create an instance of Mastodon API, where we input our access token and the API base url. In this case, where we’re running our application on the official server we input https://mastodon.social/ as the API base url. If you were running it on a different server, you would need to input the base url of that server.

m = Mastodon(
    access_token=get_token('mastodon-token'), 
    api_base_url="https://mastodon.social/"
)

And finally, we use the toot function to create a post on our Mastodon profile.

example_url = 'https://ak-codes.com/how-to-tokenize-custom-text-data-using-bert-tokenizer/'
m.toot(f"Test post via API using OAuth \n{example_url}")

That’s it! I also added a link to the post in my case here, just to demonstrate how to structure a post like that.

Here is also the entire code of this project.

import os
import json
from mastodon import Mastodon

ROOT = os.path.dirname(__file__)

def get_token(token_name):
    with open(os.path.join(ROOT, 'auth.json'), 'r') as auth_file:
        auth_data = json.load(auth_file)
        token = auth_data[token_name]
        return token

m = Mastodon(
    access_token=get_token('mastodon-token'), 
    api_base_url="https://mastodon.social/"
)

example_url = 'https://ak-codes.com/how-to-tokenize-custom-text-data-using-bert-tokenizer/'
m.toot(f"Test post via API using OAuth \n{example_url}")

Conclusion

To conclude, we made a simple Python algorithm for creating posts using Mastodon API. I learned a lot while working on this project and I hope you will find it helpful as well.

Share this article:

Related posts

Discussion(0)