Working with the Mastodon REST API with PowerShell

Image Description

Daily PowerShell #73

PowerShell Mastodon Daily PowerShell

November 10, 2022

quote Discuss this Article


Mastodon is an open source, federated social media platform. Mastodon is hosted on a growing number of servers that are typically topic or location based. Each Mastodon server can federate to other servers so, even if you join one server, you can follow and communicate with users from other servers. It’s also possible to move your account between servers or start your own server.

In this post, we’ll look at some of the REST APIs available to Mastodon developers and learn how to invoke them in PowerShell.

Public Endpoints

Public endpoints are available without providing authentication or authorization. They provide information like public accounts, statuses, trends and timelines. All endpoints are subject to rate limiting.

View Account

View a user account. You will need to know the user’s account ID which can be found with the account search functionality shown below.

Invoke-RestMethod ''

id              : 404568
username        : adamdriscoll
acct            : adamdriscoll
display_name    : Adam Driscoll
locked          : False
bot             : False
discoverable    : True
group           : False
created_at      : 8/6/2018 12:00:00 AM

View Account Statuses

View a list of statuses for an account.

Invoke-RestMethod ''

id                     : 109305372050694915
created_at             : 11/8/2022 12:33:35 AM
in_reply_to_id         :
in_reply_to_account_id :
sensitive              : False
spoiler_text           :
visibility             : public
language               : en
uri                    :
url                    :
replies_count          : 0
reblogs_count          : 0
favourites_count       : 2
edited_at              :
content                : <p>Please vote tomorrow! <a href="" target="_blank" rel="nofollow noopener noreferrer"><span class="invisible">https://</span><span class=""></span><span class="invisible"></span></a> <a
                         href="" class="mention hashtag" rel="tag">#<span>wisconsin</span></a> <a href="" class="mention hashtag" rel="tag">#<span>vote</span></a></p>

View Trends on the server.


name                url                                              history
----                ---                                              -------
followbackfriday    {@{day=1668124800; accounts=533; uses=661}, @{day=1668038400; accounts=7; uses=9}, @{day=1667952000; accounts=3; uses=3}, @{day=1667865600; accounts=0; uses=0}}
Fensterfreitag      {@{day=1668124800; accounts=300; uses=345}, @{day=1668038400; accounts=4; uses=5}, @{day=1667952000; accounts=1; uses=1}, @{day=1667865600; accounts=1; uses=2}}
followfriday        {@{day=1668124800; accounts=705; uses=931}, @{day=1668038400; accounts=34; uses=43}, @{day=1667952000; accounts=15; uses=34}, @{day=1667865600; accounts=28; uses=41}}
kevinconroy         {@{day=1668124800; accounts=110; uses=119}, @{day=1668038400; accounts=0; uses=0}, @{day=1667952000; accounts=0; uses=0}, @{day=1667865600; accounts=0; uses=0}}
veteransday         {@{day=1668124800; accounts=194; uses=227}, @{day=1668038400; accounts=8; uses=9}, @{day=1667952000; accounts=2; uses=2}, @{day=1667865600; accounts=2; uses=2}}
fursuitfriday       {@{day=1668124800; accounts=89; uses=106}, @{day=1668038400; accounts=2; uses=4}, @{day=1667952000; accounts=1; uses=1}, @{day=1667865600; accounts=1; uses=1}}
Isolationspflicht   {@{day=1668124800; accounts=60; uses=83}, @{day=1668038400; accounts=0; uses=0}, @{day=1667952000; accounts=0; uses=0}, @{day=1667865600; accounts=0; uses=0}}

Private Endpoints

Private endpoints require authentication and authorization to access. To create an access token, you’ll need to create an application. Click Preferences, Development, New Application to start creating your application. You’ll need read and write for the examples below. Once you’ve created your application, you’ll have access to your application token.

Verify Credentials

You can verify your credentials with the following endpoint. Replace my app token with yours.

Invoke-RestMethod -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }

Get Notifications

Get a list of the most recent notifications in your account.

Invoke-RestMethod -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }

Post a Status

Post a new status to your account.

Invoke-RestMethod -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" } -Body "status=Hello, from #PowerShell!! (This post was sent with Invoke-RestMethod)" -Method POST

Upload Media for a Status

Upload media and then post a status with the media include. The status text is not required when media is provided. The below example encodes the file into a HTTP body formatted as multipart/form-data and sends it to the server. Mastodon returns a media object with an ID you will need to post with your status.

$FilePath = '.\desktop\image.png';

$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";

$bodyLines = ( 
    "Content-Disposition: form-data; name=`"file`"; filename=`"image.png`";",
    "Content-Type: application/octet-stream$LF",
) -join $LF

$Media = Invoke-RestMethod -Uri -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }
Invoke-RestMethod -Body "media_ids[]=$($Media.Id)" -Method POST -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" } 

Search for a User

Search for a user. This is useful when trying to use the public endpoints for looking up user statuses and profiles.

Invoke-RestMethod -Uri '' -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }