mastodon

How do I get public user information from the Mastodon API?


I see a user like https://mastodon.lol/@rosinmas and according to the docs, the API endpoint should be: https://mastodon.lol/api/v1/accounts/@rosinmas

But I get:

{"error":"Record not found"}

How come the use endpoint isn't public?


Solution

  • You can get account information in JSON (no authentication needed) by calling the API with the following format:

    https://[server_name]/api/v1/accounts/[account_id]
    

    For example:

    https://mastodon.social/api/v1/accounts/1
    

    will give you:

    {
      "id": "1",
      "username": "Gargron",
      "acct": "Gargron",
      "display_name": "Eugen Rochko",
      "locked": false,
      "bot": false,
      "discoverable": true,
      "group": false,
      "created_at": "2016-03-16T00:00:00.000Z",
      "note": "<p>Founder, CEO and lead developer <span class=\"h-card\"><a href=\"https://mastodon.social/@Mastodon\" class=\"u-url mention\">@<span>Mastodon</span></a></span>, Germany.</p>",
      "url": "https://mastodon.social/@Gargron",
      "avatar": "https://files.mastodon.social/accounts/avatars/000/000/001/original/dc4286ceb8fab734.jpg",
      "avatar_static": "https://files.mastodon.social/accounts/avatars/000/000/001/original/dc4286ceb8fab734.jpg",
      "header": "https://files.mastodon.social/accounts/headers/000/000/001/original/3b91c9965d00888b.jpeg",
      "header_static": "https://files.mastodon.social/accounts/headers/000/000/001/original/3b91c9965d00888b.jpeg",
      "followers_count": 222337,
      "following_count": 326,
      "statuses_count": 72729,
      "last_status_at": "2022-11-18",
      "noindex": false,
      "emojis": [],
      "fields": [
        {
          "name": "Patreon",
          "value": "<a href=\"https://www.patreon.com/mastodon\" target=\"_blank\" rel=\"nofollow noopener noreferrer me\"><span class=\"invisible\">https://www.</span><span class=\"\">patreon.com/mastodon</span><span class=\"invisible\"></span></a>",
          "verified_at": null
        }
      ]
    }
    

    To get the account_id for a given username you can pass the following API call:

    https://[server_name]/api/v2/search?q=[username]
    

    For example:

    https://mastodon.social/api/v2/search?q=Gargron
    

    will give you saturated details about all the usernames with Gargron on that server...