Creating a Custom Connector for Mastodon in Power Automate

If you're not in the know, a pretty decent amount of people on Twitter have been migrating over to the Fediverse via Mastodon (including me). As this is a new platform to many, maybe you happen to have a Power Automate flow which posts a link to your latest blog post through various social media sites (me). Now you want to be able to throw Mastodon into the mix. How do we accomplish this?

Let's make a Custom Connector in Power Automate!

NOTE: You will need a Power Automate subscription to create custom connectors. You can sign up for a 90-day trial when prompted or use a developer subscription. Yes, I know exactly what you're saying right now...

Prerequisites

Getting Started

The first thing that needs to be done is creating the OAuth client app that Power Automate is going to be connect with and issue authenticated REST API calls on your behalf.

Create Client App in Mastodon

Assuming you've signed into your Mastodon instance, go to the settings page and click on the Development link.

https://{yourmastodoninstance}/settings/applications

Click the New Application button to begin.

Fill out the Application Name with your desired name. This will be displayed to identify the client where this post originated from. For example, "Web" will be the client used if you're posting from the Mastodon instance in a web browser.

Add https://global.consent.azure-apim.net/redirect on the next line in the Redirect URI section. This is the URL used when you configure your OAuth connection within Power Automate.

To post a "toot" on your timeline, the write:statuses scope is required.

Once you've finished creating the application, the Client Key and Client Secret will be available for use.

Add Custom Connector for Mastodon

For this part, we'll be in the Power Automate interface.

I'll let you choose your destiny at this point in time. Walk through the manual steps of creating a custom connector from scratch, or import the result.

From Scratch

Here's the step by step on how to create this from scratch.

Why from scratch? There's no official OpenAPI document for the Mastodon API, and the few unofficial ones that exist require so much extra manual work.

Head over to https://make.powerautomate.com. Expand the Data menu and click on Custom Connectors

Select Create from blank from the New custom connector menu

Choose a name for your connector. In this example, we'll use Mastodon API. Press Continue.

You should see a connector configuration screen with 5 steps: General, Security, Definition, Code (preview), and Test.

1. General

This step is used for configuring the base piece of the connector.

Upload Connector icon

Optional.

Icon background color

Optional.

Description

Optional, but useful to describe what this connector does.

Scheme

HTTPS

Host

Set this to the domain of the Mastodon instance you're joined to, without the https:// in front of it

Base URL

Leave this as / for now

2. Security

The security section is where you will configure authentication to the connector.

The Authentication Type portion contains a dropdown that includes many authentication provider options. Select OAuth 2.0 as the authentication option.

Fill out OAuth 2.0 configuration options:

Identity Provider

Generic OAuth 2.0

Client Id

Client Key generated from Mastodon

Client Secret

Client Secret generated from Mastodon

Authorization Url

The OAuth Authorization Url endpoint for Mastodon instances is a relative path of /oauth/authorize. For example: https://mastodon.instance/oauth/authorize

Token URL

Relative path of /oauth/token

Refresh URL

Use the Token URL for this

Scope

Use write:statuses for the scope.

Redirect URL

The Redirect URL will show once the connector is saved. It's usually the same url you configured for use in the Mastodon client app.

Your security configuration is complete.

3. Definition

You can define actions for your custom connector in the Definition section. These actions can be invoked individually from the custom connector and represent REST API calls.

For writing statuses using the Mastodon API, we will create an action that uses the statuses API. This API can be used by sending the status form-data parameter as JSON as an HTTP POST to /api/v1/statuses

POST https://mastodon.example/api/v1/statuses
Content-Type: application/json

{
    "status": "Some status text" 
}

Press the (+) New Action link under the Action section on the left.

Under General, we can use information from the statuses API to fill in the information needed for the action.

Summary

Publish new status

Description

Post a new status.

Operation ID

Post/api/v1/statuses