I wrote recently about how you can use Mastodon’s API to make bots that post random images and make data charts, but you don’t have to automate just bots. Let me show you a few fun things you can do with your own profile.
First, we’ll need to get an API access token. The process is the same as when setting up a bot, but you will do this with your own account.
Next, we’ll need to sign up for a free Pipedream.com account. Once we’re in, let’s head to the settings page and save our access token and our Mastodon instance as environmental variables.


MASTODON_ACCESS_TOKEN = "12345_abcdefghijklmno"
MASTODON_INSTANCE = "https://mastodon.social"
All done, now let’s create a new workflow.

Let’s pick Schedule as a trigger with the Custom Interval option.


Next, we’ll add a Python code block that will run every time our scheduler is triggered.




Let’s test out the API with the following code:
import os
from mastodon import Mastodon
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get("MASTODON_ACCESS_TOKEN"), api_base_url = os.environ.get("MASTODON_INSTANCE"), request_timeout = 100)
post = mastodon.status_post("Helo world!")
return post
Go ahead and click the Test button.
You should see a confirmation that everything run successfully, and you will also see your test message posted on your account.


Great! Now, let’s have a look at the Mastodon API documentation to see what else we can do.
Let’s play around with the accounts/update_credentials
endpoint. Mastodon.py has a matching Mastodon.account_update_credentials
method, which looks like this:
Mastodon.account_update_credentials(display_name=None, note=None, avatar=None, avatar_mime_type=None, header=None, header_mime_type=None, locked=None, bot=None, discoverable=None, fields=None)
Here’s how we can use it to update our display name on Mastodon.
import os
from mastodon import Mastodon
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get("MASTODON_ACCESS_TOKEN"), api_base_url = os.environ.get("MASTODON_INSTANCE"), request_timeout = 100)
result = mastodon.account_update_credentials(display_name="Playing with Mastodon API")
return result
Click the Test button and check your profile page.

So far, so good. Note that you will get an error if you try to use text that’s over the length limit, currently 30 characters.
Let’s try updating the profile fields. First, we will need to retrieve the current values.
import os
from mastodon import Mastodon
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get("MASTODON_ACCESS_TOKEN"), api_base_url = os.environ.get("MASTODON_INSTANCE"), request_timeout = 100)
result = mastodon.account_verify_credentials()
print(result['fields'])
return result
And here are the results.

Mastodon.py’s account_update_credentials
function only works with tuples, and it does not accept the verified_at
attribute, so we have to tidy up the returned object a bit.
Here’s my full, annotated code.
import os
import datetime, pytz
from mastodon import Mastodon
from pprint import pprint
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get('MASTODON_ACCESS_TOKEN'), api_base_url = os.environ.get('MASTODON_INSTANCE'), request_timeout = 100)
# First, let's retrieve the current profile fields.
result = mastodon.account_verify_credentials()
profile_fields = result['source']['fields']
# Now, let's create our new field.
custom_field_name = 'Local time'
custom_field_new_value = datetime.datetime.now(pytz.timezone('US/Eastern')).strftime('%I:%M %p')
custom_field_exists = False
# Keep in mind that you can only have 4 fields on your profile, so you might have to delete one from your profile, if necessary.
for field in profile_fields:
# Remove the verified_at attribute to keep Mastodon.py happy.
del field['verified_at']
# If the field already exists, we can update the value.
if field['name'] == custom_field_name:
custom_field_exists = True
field['value'] = custom_field_new_value
# Otherwise we'll add a new field.
if custom_field_exists == False:
profile_fields.append({
'name': custom_field_name,
'value': custom_field_new_value
})
# We need to convert the dictionary Mastodon returns to a tuple, which is a data structure Mastodon.py uses for profile fields.
profile_fields_tuple = []
for row in profile_fields:
profile_fields_tuple.append(tuple(list(row.values())))
# And finally, we can update the profile fields.
result = mastodon.account_update_credentials(fields = profile_fields_tuple)
return result
You can now press the “Test” button, and after a brief moment you should see your profile fields updated.

Note that Mastodon will cache your profile fields for at least a few minutes (shout-out to Ben Tasker for asking about this), so showing local time will not be always accurate, but this is fine for our demo.
Great, we got the hard part taken care of. Now you can adjust the time interval, and hit the “Deploy” button to schedule your workflow.
And if you need to, you can pause this workflow back on the workflows page.

Let’s try one more thing with the user fields. Say we also want to show the weather in our location. For a quick demo, we can use the free Open-Meteo site, which has a really nice URL builder. Fill in your city, pick your temperature unit, make sure to check the “Current weather with temperature, windspeed and weather code” option, and click the Preview Chart button.
You should then see a URL below the chart that looks like this.
https://api.open-meteo.com/v1/forecast?latitude=40.71&longitude=-74.01&hourly=temperature_2m¤t_weather=true&temperature_unit=fahrenheit
Here’s the full updated code.
import os
import requests
import datetime, pytz
from mastodon import Mastodon
from pprint import pprint
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get('MASTODON_ACCESS_TOKEN'), api_base_url = os.environ.get('MASTODON_INSTANCE'), request_timeout = 100)
result = mastodon.account_verify_credentials()
profile_fields = result['source']['fields']
custom_field_name = 'Time and weather'
current_time = datetime.datetime.now(pytz.timezone('US/Eastern')).strftime('%I:%M %p')
response = requests.get('https://api.open-meteo.com/v1/forecast?latitude=40.71&longitude=-74.01&hourly=temperature_2m¤t_weather=true&temperature_unit=fahrenheit')
response_json = response.json()
current_weather = response_json['current_weather']
current_temperature = current_weather['temperature']
custom_field_new_value = f"{current_time} | {current_temperature}°F"
custom_field_exists = False
for field in profile_fields:
del field['verified_at']
if field['name'] == custom_field_name:
custom_field_exists = True
field['value'] = custom_field_new_value
if custom_field_exists == False:
profile_fields.append({
'name': custom_field_name,
'value': custom_field_new_value
})
profile_fields_tuple = []
for row in profile_fields:
profile_fields_tuple.append(tuple(list(row.values())))
result = mastodon.account_update_credentials(fields = profile_fields_tuple)
return result
And here is the result.

Some other ideas to try here:
- Pick a random quote
- Show a song you’re listening to
- Your Wordle score
- Your current live location
Okay, maybe the last one is not a great idea. But you get the point. There’s quite a lot you can do here.
And to wrap things up, let’s look at how we can upload a random new header image to our profile. For this example, I am going to use a few protest images collected in this Magenta.as blog post.
Here’s the full code.
import os
import random
import urllib.request
from mastodon import Mastodon
from pprint import pprint
def handler(pd: "pipedream"):
mastodon = Mastodon(access_token = os.environ.get('MASTODON_ACCESS_TOKEN'), api_base_url = os.environ.get('MASTODON_INSTANCE'), request_timeout = 100)
image_urls = [
'https://static.stefanbohacek.dev/images/power-to-the-people/01-banner.PNG',
'https://static.stefanbohacek.dev/images/power-to-the-people/02-banner.PNG',
'https://static.stefanbohacek.dev/images/power-to-the-people/03-banner.PNG',
'https://static.stefanbohacek.dev/images/power-to-the-people/04-banner.PNG',
]
image_url = random.choice(image_urls)
filename = "/tmp/image.png"
urllib.request.urlretrieve(image_url, filename)
result = mastodon.account_update_credentials(header = filename)
return result
You could also combine this with the example above and add an image credit in one of your profile fields. And instead of managing your image URLs in your code, you could follow this tutorial that uses Google Drive.
Hope you found this tutorial useful! Here’s a link to a finished workflow with one of the examples from this article. And if you have any questions or suggestions, feel free to reach out.
Until next time!
PS: Shout out Leon Cowle who used this tutorial to write a Pipedream-compatible version of his Python script that monitors your Mastodon lists and warns you when someone silently drops out (which will happen when one of your lists’ members moves to a new instance).