iTranslated by AI
How to Toot from Python using Mastodon.py
With the Twitter API becoming a paid service, it seems it will be difficult to continue operating personal Twitter bots. Therefore, I tried implementing Toots from Python using Mastodon.py, a Python wrapper for the Mastodon API.
Environment Setup
Python Environment Setup
If your Python development environment is not yet set up, please refer to the following articles to set it up.
Install Mastodon.py
Install Mastodon.py using pip.
pip install Mastodon.py
Obtaining a Mastodon Account
Prepare a Mastodon account to operate as a bot.
One thing to note here is that terms of service vary by Mastodon instance, and bot operations may be restricted or have specific rules.
It is best to create a bot account after reading the instance's terms of service thoroughly.
For example, as of February 4, 2023, mstdn.jp, the largest instance in Japan, has the following rules for bot operation:
・Do not make Public posts (Please use Unlisted, Private, or Direct)
・Do not actively follow other accounts
・Do not automatically favorite or reply to toots from people you do not follow
The reasons and other details are listed in the terms of service, so please take a look.
If there seem to be no issues with your intended use or operation method after reading the terms, go ahead and create an account.
Configuring the Post Visibility of your Mastodon Account
Once the account is created, change the Mastodon account settings.
Open the User Settings / Other configuration items and change the Post Privacy.

By default, it is set to Public, so change it to a setting based on the terms of service.
For example, in the case of mstdn.jp, the rule states to "use Unlisted, Private, or Direct," so set the visibility level to Unlisted or lower.
Registering a New Application with a Mastodon Account
To operate the account via the Mastodon API, you need to register an application.

Select the Development item from the left menu on the settings screen.

The New Application button will appear. Click it to set the application name and the types of permissions.
Once the settings are complete, the following will be displayed:
- Client Key
- Client Secret
- Access Token
Be sure to save these strings somewhere.
That's all for the necessary preparations before implementation.
Implementation
Now, let's proceed with the implementation. Here is the official documentation for Mastodon.py.
I would like to say let's implement it according to the Usage section in the official documentation, but executing the create_app method as described there may cause a Request Error depending on the instance.
This seems to be because some instances block requests based on the UserAgent.
This issue is recognized by the Mastodon.py side, and a pull request for a fix has been submitted.
However, since running the create_app method is not actually mandatory for automated tooting on Mastodon, we will proceed with the implementation while disregarding the Usage section for now.
From Initialization to Tooting
Now, let's write the code from initialization to tooting.
If you have already created the application on the web, there is no need to use create_app.
By not using create_app, the aforementioned UserAgent issues can be avoided.
By specifying the
- Client Key
- Client Secret
- Access Token
prepared during the environment setup as arguments when creating the class instance, you can toot any text simply by calling the toot method.
from mastodon import Mastodon
# Initialization
api = Mastodon(
api_base_url = 'Mastodon instance URL (e.g., https://mstdn.jp)',
client_id = 'Client Key issued during web app registration',
client_secret = 'Client Secret issued during web app registration',
access_token = 'Access Token issued during web app registration'
)
# Toot
api.toot('Hello World')
That's all.
The main stumbling block when using Mastodon.py is likely centered around create_app, so once you avoid that and manage to initialize successfully, most methods should be executable.
Finally, please try various things by referring to the API METHODS described in the official documentation.
Discussion