mcjones.ca

Mastodon Client Introduction

General

Golang

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/mattn/go-mastodon"
)

func main() {
	c := mastodon.NewClient(&mastodon.Config{
		Server: "https://example.com",
		AccessToken: "ACCESS_TOKEN",
	})

	// Print mastodon version
	mastodonVersion, err := c.GetInstance(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(mastodonVersion.Version)

	//Toot and delete
	information, err := c.PostStatus(context.Background(), &mastodon.Toot{
		Status: "API Test",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(information)

	err = c.DeleteStatus(context.Background(), information.ID)
	if err != nil {
		log.Fatal(err)
	}
}

Python

pip install Mastodon.py
client_id/key
client_secret
URL
Name of APP

If you have an access token (eg. created an app on the web UI) - you can initialize with the access token.

from mastodon import Mastodon

mastodon = Mastodon(
    api_base_url = 'https://example.com',
    access_token = 'ACCESS_TOKEN'
)

print ('Mastodon version: ' + mastodon.retrieve_mastodon_version())

information = mastodon.status_post(
    status = "Status via the API",
)

print (information)

mastodon.status_delete(
    id = information['id']
)