Toot on Mastodon using API and PowerShell

I wrote blog posts about how to use an API for Slack and Twitter messages, but… Mastodon is also there now, and I thought… Ok, can I Toot in Mastodon using PowerShell? This blog post will show you how 😉

What is Mastodon?

“Mastodon is a decentralized social network made up of independent servers organized around specific themes, topics, or interests. People can join servers, follow each other, engage in conversations, and otherwise do all sorts of things they’d expect to do on a social network like Twitter” (Source)

What is a ‘Toot’?

“A toot is basically Mastodon’s name for a tweet; it’s a post that can be shared within one of the aforementioned servers. Unlike Twitter, though, the maximum toot length is 500 characters, giving users a bit more space to work with. It also includes some other features, like clickable spoiler warnings.” (Source)

Preparation

As a logical first step, you should have a Mastodon account. Creating one differs from other platforms like Slack or Twitter, and Mastodon requires you to pick a server to register on first. You can do that by visiting https://joinmastodon.org/ and selecting Create account. Pick a server based on your preference/target audience. I selected Cyberspace.Social Server for this blog post, after registering and confirming your email address, it will take you to something like this:

Registering an application for API access

To register an application, please follow these steps:

  • Select Preferences on the right.
  • Select Development in the left side menu.
  • Select New Application.
  • Enter an Application name. In my case, I entered PowerShellisfun as the Application name.
  • Deselect read and follow and select write:statuses
  • Scroll down and select Submit to save the application

You should see an “Application successfully created” banner on the top of the Your Applications page. Select your application and note the Your access token. You will need this in the next step for creating your first Toot.

Create your first Toot

I found an excellent how-to here. It showed how to Toot using Curl, which I used to rebuild into an Invoke-Webrequest, which looks like this: (Replace XXXXX with your access token)

$message = 'PowerShell is fun :)'
$url = 'https://cyberplace.social/api/v1/statuses'
$token = 'XXXXX'
$headers = @{
    "Authorization" = "Bearer $($token)"
}
$body = @{
    status = $message
}

Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $body

After running this command, you should see an HTTP/1.1 200 OK in your session:

StatusCode        : 200                                                                                                                                                                                                                                                   StatusDescription : OK                                                                                                                                                                                                                                                    
Content           : {"id":"109820240356384973","created_at":"2023-02-06T22:51:22.392Z","in_reply_to_id":null,"in_reply_to_account_id":null,"
                    sensitive":false,"spoiler_text":"","visibility":"public","language":"en","uri":"...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Vary: Accept-Encoding,Origin
                    X-Frame-Options: DENY
                    X-Content-Type-Options: nosniff
                    X-XSS-Protection: 0
                    Permissions-Policy: inte...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [Vary, Accept-Encoding,Origin], [X-Frame-Options, DENY]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 1427

This should result in your first API Toot 🙂

More Mastodon API information

You can find more information about the API here.

2 thoughts on “Toot on Mastodon using API and PowerShell

Leave a Reply to Steven RoggeCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.