Mastodon.py is a Python library that I maintain that aims to implement the full breadth of the API for the Mastodon microblogging platform. It is now 10 years old - the first commit was at Wed Nov 23 23:29:30 2016 +0100:

Mastodon.pys initial commit, dated as above.

That’s a long time to be doing anything! So for this anniversary, I have for you not only an Anniversary Release, but also some Mastodon.py facts:

Mastodon.py has ca. 3000 dependents on GitHub, maybe a few more in other places

Most of those are forks of HomeAssistant, which has a Mastodon posting module that uses it. 650 or so are other things, like creative bots, crossposters1, feed posters, integration into some other system like a blog or some sensing device, frameworks, analytics and archiving tools, full featured clients and tools for feed curation, probably more categories…

I don’t have telemetry to tell me, obviously, but I would guessestimate that there’d have to be at least a couple hundred, maybe low thousands posts that get sent into the Fediverse via Mastodon.py each day. It’s also pretty likely that quite a few of the annoying spambots you see pop up from time to time use it, especially now, given that LLMs will reach for Python as the hammer for every conceivable nail. Sorry about that!

It was originally written specifically for Quasicrystals Bot

Quasicrystals Bot existed as a Twitter2 bot at the time, and I wanted to have it posting to Mastodon (first in addition to, and later instead of on, Twitter). No one had written a Python library to interact with the API that I could find, so I wrote one for myself, and why not share it with the world to try and encourage others to also port their bots, right?

Me asking if someone has already implemented a Python wrapper for the API, getting a response of Nope, and deciding that I will do it myself.

The third great virtue of a programmer on full display.

I apparently originally wrote it inside a Jupyter notebook

The initial commit3 has some test cells committed after I exported it.

Some test code accidentally part of Mastodon.pys initial commit, but the actual one.

My commit discipline on this particular project has since improved, but not by much. I swear I can do things properly if needed.

Surprisingly, quite a bit of that code in some form or another has survived to the current version of the library without a major rewrite!

The initial huge batch of tests and docs came from like three people

Testing was initially added by @wjt for the streaming code that he also contributed, and was later expanded by (thanks) @codl to cover most of what existed then in terms of features. The docs were written originally by @lydia.

All of this has since been kept in shape by me and others. Mastodon.py has tests for almost all functionality that is ran against an actual Mastodon server to validate our assumptions with 92% line coverage, and docs that cover everything you can do, occassionally in more detail than the actual Mastodon API docs (less so after those were massively improved by @trwnh4), which unfortunately sometimes don’t get as much love as they deserve.

I super burned out on a major refactor

At some point, someone suggested to add proper types to Mastodon.py. This is obviously a good idea, so I got started. But as it turns out, this was a massive effort. And while I was trying to get it sorted out, new Mastodon versions came out, meaning more work that I felt I should probably do before tagging a release. And so on. So from mid 2023, there just isn’t a new release until 2025, when I decided to essentially take a week off from work and Sort This Shit Out, and now it’s generally back on track with semi-regular releases and also a bunch of process improvements that make releasing new versions easier.

Almost 80 people have contributed something to it

Be that via reporting issues or contributing some code. And thank you all - if you’re reading this, and you reported an issue, or contributed code - thank you so much for helping to make the library a bit better5.

I almost gave a FOSDEM talk about it, but I couldn’t really figure out what to say

I do slightly regret not agreeing to6, but I just really struggled to think what I’d say in a 15 minute slot that would be interesting to anyone - that’s just about enough time to go through the most basic tutorial possible, and the docs already start with that. The library is a bit too basic and, I think, a bit too easy to use in the most basic capacity to say much about it7! Which is actually a good thing, probably, and…

You should make a creative bot!

…this neatly seques into a call to action, I think: I want you to make more creative bots. I love weird bots that generate An Art and post it, or that you can get some personalized text from, or that post frames from your favourite movie, or &c &c. The possibilities are endless, and Mastodon.py genuinely is extremely simple to use. The opening of the docs gets you most of the way there already, and there’s a repo with examples here now!

Here, I’ll do it for you inline. This is literally all the scaffolding you need for a basic bot:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from mastodon import Mastodon
import os

# Create app
url = input("Instance URL: ")
if not os.path.exists("client.secret"):
    Mastodon.create_app(
        "Mastodon.py test", 
        api_base_url=url, 
        to_file="client.secret"
    )

# Log in
if not os.path.exists("user.secret"):
    m = Mastodon(client_id="client.secret", api_base_url=url)
    print(m.auth_request_url())
    m.log_in(code=input("OAuth code: "), to_file="user.secret")

# Post
m = Mastodon(access_token="user.secret")
m.status_post("Hello! ( ・ω・)ノ")

Run it on your PC and have it post only when you’re there, run it on a server, put it in cron, put it in a webpage, explore the docs for additional stuff you might wish to do, the sky truly is the limit. Tell me about it if you do, I love to hear about fun weird bots. Or if you have any problems or suggestions, please open an issue, or just @ me8 - we can probably get it sorted out!

Finally

Maintaining an even relatively modestly sized open source project is annoying, but at least sometimes rewarding (and in this case, I do find myself still reaching for the library pretty regularly, so it’s not like I’m getting nothing here personally either!). It is really fun to see what people build with the thing you make. Maybe some day me, or someone else, can use Mastodon.py to build the web-of-trusty, doesn’t-require-handing-power-to-a-central-organization, stops-spam-and-harassment-but-doesn’t-encourage-centralization-by-excluding-new-servers moderation tooling of our dreams, and thusly, redeem us for those (potential) spam bots.



  1. First from/to twitter, then from/to bsky, now probably mostly pointless. ↩

  2. For our younger readers: This is the original name of Elon Musks X Dot Com The Everything App. It used to have an API just marginally worse than Mastodons at the time. ↩

  3. The actual initial commit. There’s two initial commits because I was working locally and did an initial commit with that message but then initialized a repo on GitHub to push to which also has the same message. Whoopsie~ ↩

  4. Neither Lydia nor trwnh currently still use Mastodon, or at least not under those names. Thank you both so, so much for your contributions to our and Mastodons docs, if you happen to see this. ↩

  5. As maintainer, I’m still by far the biggest contributor - though the situation isn’t quite as drastic as it appears to be, since counts are inflated by adding and removing VCR.py cassette tests. ↩

  6. Though I also don’t really have that much spending money for stuff like this, and Belgium’s a flight and vacation days from here, so It Is What It Is, anyways. ↩

  7. Also, this was somewhere towards the end of being burned out on maintenance, so I felt pretty bad about not having released an update with support for any of the new Mastodon features in like 1.5 years ↩

  8. In [Present Year], I feel it difficult to still say “no stupid questions”, but if you are a person worrying about whether your question could be stupid, it definitely is not and you should ask it. ↩