mastodon_client 1.0.0-beta.1 copy "mastodon_client: ^1.0.0-beta.1" to clipboard
mastodon_client: ^1.0.0-beta.1 copied to clipboard

A pure Dart Mastodon API client. Covers accounts, statuses, timelines, notifications, media, filters, and admin APIs with OAuth, pagination, and typed error handling.

example/example.dart

// ignore_for_file: unused_local_variable, avoid_print

import 'package:mastodon_client/mastodon_client.dart';

void main() async {
  // -------------------------------------------------------
  // 1. Initialize the client
  // -------------------------------------------------------
  final client = MastodonClient(
    baseUrl: 'https://mastodon.social',
    accessToken: 'YOUR_ACCESS_TOKEN',
  );

  // -------------------------------------------------------
  // 2. Fetch server info (no authentication required)
  // -------------------------------------------------------
  final instance = await client.instance.fetch();
  print('Server: ${instance.title}');
  print('Version: ${instance.version}');

  // -------------------------------------------------------
  // 3. Verify the authenticated user
  // -------------------------------------------------------
  final me = await client.accounts.verifyCredentials();
  print('Logged in as: ${me.acct}');

  // -------------------------------------------------------
  // 4. Post a status
  // -------------------------------------------------------
  final result = await client.statuses.create(
    const MastodonStatusCreateRequest(
      status: 'Hello from mastodon_client!',
    ),
  );
  if (result case MastodonStatusCreated(:final status)) {
    print('Posted: ${status.url}');
  }

  // -------------------------------------------------------
  // 5. Fetch home timeline with pagination
  // -------------------------------------------------------
  final page = await client.timelines.fetchHome(limit: 20);
  for (final status in page.items) {
    print('${status.account.acct}: ${status.content}');
  }

  // Fetch the next page
  if (page.nextMaxId != null) {
    final nextPage = await client.timelines.fetchHome(
      limit: 20,
      maxId: page.nextMaxId,
    );
  }

  // -------------------------------------------------------
  // 6. Error handling
  // -------------------------------------------------------
  try {
    await client.accounts.fetchById('unknown_id');
  } on MastodonNotFoundException {
    print('Account not found');
  } on MastodonUnauthorizedException {
    print('Invalid token');
  } on MastodonRateLimitException catch (e) {
    print('Rate limited — retry after ${e.retryAfter}');
  } on MastodonException catch (e) {
    print('Error: $e');
  }
}
0
likes
150
points
57
downloads

Documentation

Documentation
API reference

Publisher

verified publisherlibrarylibrarian.com

Weekly Downloads

A pure Dart Mastodon API client. Covers accounts, statuses, timelines, notifications, media, filters, and admin APIs with OAuth, pagination, and typed error handling.

Repository (GitHub)
View/report issues

Topics

#mastodon #fediverse #api-client #social-media #activitypub

License

BSD-3-Clause (license)

Dependencies

crypto, dio, json_annotation, logger

More

Packages that depend on mastodon_client