Tooted on
---
title: "Displaying a Toot in Hugo"
date: 2022-05-20T16:57:11-04:00
draft: false
tags: ["Hugo"]
math: false
---
Mastodon for me is a nice friendly place and I enjoy participating in that community. With that, I want to be able to share the great toots out there in my own website as well as keep an archive of all the toots I made. This post will go over the code I wrote in Hugo to display a single toot into a blog post.
Example toot:
{{< displayToot fosstodon-org-108334900197768307 >}}
In this post, we will go over the following:
- [Mastodon API](#mastodon-api)
- [Displaying Toot](#displaying-toot)
- [Hugo Shortcode](#hugo-shortcode)
- [Archiving Toots](#archiving-toots)
- [Conclusion](#conclusion)
## Mastodon API
Before jumping into the code right away, it is good to understand how to query data from Mastodon. The Mastodon API is so friendly that for public toots, you don't even need an API key. The first thing we need to know is the toot id of the toot you are interested in. In my example above, the toot id is `108334900197768307`, you can get that by looking at the last string of numbers at the end of the toot URL. You can get the toot URL by clicking on "Copy link to post" in the three dot menu of the toot. Otherwise you can get it from the URL of the favorite and reblog button. Let's say the URL of the toot is the following:
```
https://fosstodon.org/@brozek/108334900197768307
```
Then the API endpoint to get the JSON representation is at:
```
https://fosstodon.org/api/v1/statuses/108334900197768307
```
In its most general form you'll need to know the server the toot came from and its id:
```
https://SERVER/api/v1/statuses/TOOTID
```
If you check out the example link, you'll see that the JSON returned is of the form:
{{< details "Expand JSON:">}}
```json
{
"id": "108334900197768307",
"created_at": "2022-05-20T15:09:50.226Z",
"in_reply_to_id": "108331903834519586",
"in_reply_to_account_id": "108219415927856966",
"sensitive": false,
"spoiler_text": "",
"visibility": "unlisted",
"language": "en",
"uri": "https://fosstodon.org/users/brozek/statuses/108334900197768307",
"url": "https://fosstodon.org/@brozek/108334900197768307",
"replies_count": 0,
"reblogs_count": 0,
"favourites_count": 0,
"edited_at": null,
"content": "[Shortened for display purposes]",
"reblog": null,
"application": null,
"account": {
"id": "108219415927856966",
"username": "brozek",
"acct": "brozek",
"display_name": "Brandon Rozek",
"created_at": "2022-04-30T00:00:00.000Z",
"note": "[Shortened for display purposes]",
"url": "https://fosstodon.org/@brozek",
"avatar": "https://cdn.fosstodon.org/accounts/avatars/.../c007afd0c6749859.png",
"header": "https://fosstodon.org/headers/original/missing.png",
"followers_count": 114,
"following_count": 271,
"statuses_count": 57,
"last_status_at": "2022-05-20",
"emojis": [ ],
"fields": [ ]
},
"media_attachments": [],
"mentions": [
{
"id": "106627708559095741",
"username": "ashshuota",
"url": "https://fosstodon.org/@ashshuota",
"acct": "ashshuota"
},
{
"id": "107584265842142303",
"username": "technicalissues",
"url": "https://fosstodon.org/@technicalissues",
"acct": "technicalissues"
}
],
"tags": [],
"emojis": [],
"card": null,
"poll": null
}
```
{{< /details >}}
Wow that's a lot of information that comes with a single toot! For the purposes of my simple toot displayer I will only focus on a few of the fields:
- `account.avatar`: Avatar image of tooter
- `created_at`: Date toot was posted
- `media_attachments`: Images within the toot (if any)
- `url`: URL of toot
- `content`: Contents of toot
For sake of simplicity, let's ignore content warnings and boosts and revisit those in a future post.
## Displaying Toot
The following is the high level overview of our toots, complete with microformats2 metadata.
```html
Tooted on
Tooted on