Working with the Mastodon REST API with PowerShell

Image Description

Daily PowerShell #73

PowerShell Mastodon Daily PowerShell

November 10, 2022

quote Discuss this Article

About

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 'https://mastodon.social/api/v1/accounts/404568'

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 'https://mastodon.social/api/v1/accounts/404568/statuses'

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                    : https://mastodon.social/users/adamdriscoll/statuses/109305372050694915
url                    : https://mastodon.social/@adamdriscoll/109305372050694915
replies_count          : 0
reblogs_count          : 0
favourites_count       : 2
edited_at              :
content                : <p>Please vote tomorrow! <a href="https://myvote.wi.gov/en-us/" target="_blank" rel="nofollow noopener noreferrer"><span class="invisible">https://</span><span class="">myvote.wi.gov/en-us/</span><span class="invisible"></span></a> <a
                         href="https://mastodon.social/tags/wisconsin" class="mention hashtag" rel="tag">#<span>wisconsin</span></a> <a href="https://mastodon.social/tags/vote" class="mention hashtag" rel="tag">#<span>vote</span></a></p>

View Trends on the server.

Invoke-RestMethod https://mastodon.social/api/v1/trends

name                url                                              history
----                ---                                              -------
followbackfriday    https://mastodon.social/tags/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      https://mastodon.social/tags/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        https://mastodon.social/tags/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         https://mastodon.social/tags/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         https://mastodon.social/tags/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       https://mastodon.social/tags/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   https://mastodon.social/tags/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 https://mastodon.social/api/v1/accounts/verify_credentials -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }

Get Notifications

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

Invoke-RestMethod https://mastodon.social/api/v1/notifications -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }

Post a Status

Post a new status to your account.

Invoke-RestMethod https://mastodon.social/api/v1/statuses -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 = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"image.png`";",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF"
) -join $LF

$Media = Invoke-RestMethod -Uri https://mastodon.social/api/v1/media -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }
Invoke-RestMethod https://mastodon.social/api/v1/statuses -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 'https://mastodon.social/api/v1/accounts/search?q=adamdriscoll@mastodon.social' -Headers @{ Authorization = "Bearer NDobIMP6bK0lA-xkOQ6XKqyhuJXHHv8p6yLqchAVhwQ" }