June 2, 2023

How to write a shell script for boosting Mastodon post from the commandline

When talking to a broad, english speaking audience on Mastodon, then that audience is clustered around three major timezones with 9 hours in between them. There's no single time during the day at which publishing a new post will reach everyone. That's where boosting comes in.

Scheduled boosts is something, I will probably add to Fedimeister eventually. Till then, this blog post is both, an instruction as well as a reminder.

I will be sticking to tools, readily available for Linux, Windows and MacOS: curl for communication with the Mastodon server and, optionally, either json_pp or firefox to reformat JSON output to something human readable.

Step 1: Authorization

Most Mastodon API calls need an OAUTH2 access token as authorization. While it is possible to obtain the token using curl, it is far easier to log the account in with fedimeister and copy the userToken value from the 🗋 config/profiles/default.prf file.

OAUTH2 and Mastodon

The OAUTH2 flow is time sensitive and requires registration of the client application. The way it is implemented in Mastodon makes using a webbrowser mandatory.

Confirm that you have the correct token by asking your instance to verify it:

export INSTANCE="https://mastodon.example.com"
export TOKEN=your_access_token
curl -H "Authorization: Bearer $TOKEN" $INSTANCE/api/v1/accounts/verify_credentials

This will print all of the account settings in JSON format. The token is reusable, so you only need to do this step once.

Step 2: Getting id of a post

First obtain the url of the post to boost by opening it in a browser and either copy the address bar or select Copy link to post from the post’s menu.

The url needs then to be resolved to a database ID. Do not attempt to parse it. This will fail if the post originates from another instance. Do a search request instead:

export INSTANCE="https://mastodon.example.com"
export TOKEN=your_access_token
export URL="https://mastodon.example.com/@..."
curl -H "Authorization: Bearer $TOKEN" $INSTANCE/api/v2/search?resolve=true&type=statuses&q=$URL

The result will be a JSON structure (either safe it to a file and view it in Firefox or pipe it through json_pp ). Copy the value of the id field in the first entry of the statuses list (there should only be one entry).

This step needs to be done once for every post, you are going to boost.

Step 3: Boosting the post

Once you got the post’s ID and your OAUTH2 token, boosting is just a simple HTTP GET request.

export INSTANCE="https://mastodon.example.com"
export TOKEN=your_access_token
export ID="see_above"
curl -X POST -H "Authorization: Bearer $TOKEN" $INSTANCE/api/v1/statuses/$ID/reblog