Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

tsl-mastodon-api

typescriptlibs25.3kMIT0.5.1TypeScript support: included

TypeScript Library for the Mastodon API

Mastodon, TypeScript

readme

TypeScript Library for the Mastodon API

TypeScript library to access a Mastodon server from front-end or back-end environments.

CodeQL Node.js npm license

Installation

Run the following command for client/server or server-only projects:

npm install tsl-mastodon-api

Run the following command for client-only projects:

npm install tsl-mastodon-api --omit=optional

Access Token

You need an access token for communication with a Mastodon server.

  1. Open your Mastodon website and go to the profile settings of the account.

  2. In the profile settings open the section 'Development'.

  3. Create a new application and use the related access token.

Or you can use the OAuth.createApp function (import tsl-mastodon-api/lib/OAuth.js).

Examples

import * as Mastodon from 'tsl-mastodon-api';
async function postHelloWorld(): Promise<void> {
    // create the API instance
    const mastodon = new Mastodon.API({
        access_token: 'ABC',
        api_url: 'https://mastodon.example/api/v1/'
    });
    // expect client / server errors
    try {
        const media = await mastodon.postMediaAttachment(
            { file: await Mastodon.Utilities.fileFrom('animation.gif') },
            true
        );
        const result = await mastodon.postStatus({
            media_ids: [media.json.id],
            sensitive: true,
            spoiler_text: 'Hello',
            status: 'World'
        });
        console.log(JSON.stringify(result));
    }
    catch (error) {
        console.error(error);
    }
}
postHelloWorld();

API Overview

The following snippets show an excerpt of the API.

API(config)
API.delay()
API.search(search)

API.getAccount()
API.getAnnouncements(queryParameters?)
API.getListAccounts(listID, queryParameters?)
API.getMediaAttachment(mediaAttachmentID, awaitProcessing)
API.getNotifications()
API.getStatusesOfPublic(queryParameters?)

API.postDismissAnnouncement(announcementID)
API.postDismissNotification(notificationID)
API.postListAccounts(listID, listAccounts)
API.postMediaAttachment(mediaAttachment, awaitProcessing)
API.postPollVote(pollID, pollVote)
API.postStatus(status)

API.putAnnouncementReaction(announcementID, emojiName)
API.putMediaAttachmentUpdate(mediaAttachmentID, mediaAttachmentUpdate)

API.deleteAnnouncementReaction(announcementID, emojiName)
API.deleteListAccounts(listID, listAccounts)
API.deleteNotification(notificationID)
API.deleteStatus(statusID)
JSON.isAccount(json)
JSON.isAnnouncement(json)
JSON.isList(json)
JSON.isMediaAttachment(json)
JSON.isNotification(json)
JSON.isStatus(json)
JSON.isStreamData(json)
REST(config)
REST.delete(path, params?)
REST.fetch(method, path, params?)
REST.get(path, params?)
REST.patch(path, params?)
REST.post(path, params?)
REST.put(path, params?)

Optional with import of tsl-mastodon-api/lib/OAuth.js:

OAuth.createApp(apiURL, appName, redirectURI?, scopes?, website?)
OAuth.getAccessToken(baseURL, clientId, clientSecret, authorizationCode, redirectUri?)
OAuth.getAuthorizationUrl(baseURL, clientId, clientSecret, redirectUri?, scope?)

Optional with import of tsl-mastodon-api/lib/StreamAPI.js:

StreamAPI(config)
StreamAPI.off(eventType, eventListener)
StreamAPI.on(eventType, eventListener)
StreamAPI.subscribe(streamType, streamParams?, eventListener?)
StreamAPI.unsubscribe(streamType, streamParams?, eventListener?)

changelog

Changelog

v0.5.1

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.5.0...v0.5.1

v0.5.0

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.4.2...v0.5.0

v0.4.2

  • Fix form data with overloaded key
  • Fix issue to add media attachment

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.4.1...v0.4.2

v0.4.1

  • NotificationParams
  • Remove focus meta as requirement for image media

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.4.0...v0.4.1

v0.4.0

  • Added API.delete
  • Added API.deleteAnnouncementReaction
  • Added API.get
  • Added API.getAnnouncements
  • Added API.post
  • Added API.postDismissAllNotifications
  • Added API.postDismissAnnouncement
  • Added API.put
  • Added API.putAnnouncementReaction
  • Added API.version and API.Config.api_version
  • Added Bridge.WebSocket
  • Added JSON doc comments
  • Added JSON.Announcement
  • Added JSON.isEmojis
  • Added JSON.Reaction
  • Added JSON.StreamData
  • Added StreamAPI
  • Added ws package as optional dependency for server-side WebSocket
  • Added node-fetch package as optional dependency for server-side fetch (< Node.js v18)
  • Added oauth package as optional dependency for server-side OAuth
  • Renamed API.deleteNotification to API.postDismissNotification
  • Fixed #25, dismiss notification should be API.post

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.3.0...v0.4.0

v0.3.0

  • Added API.deleteNotification (@mindlapse)
  • Added API.getStatusContext (@sophiebremer)
  • Added API.getStatusesOf... timeline functions (@sophiebremer)
  • Added JSON.isStatusContext (@sophiebremer)
  • Added JSON.isTags (@sophiebremer)
  • Improved JSON.isStatus to validate tags (@sophiebremer)
  • Improved REST.fetch to not post empty form data (@mindlapse)
  • Improved array validations (@sophiebremer)
  • Fixed JSON.isTag expecting mandatory history property (@sophiebremer)

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.2.0...v0.3.0

v0.2.0

  • Added bridge to browsers and Node.js (@sophiebremer)
  • Added notifications support (@mindlapse)
  • Added support for duplicated params keys (@mindlapse)
  • Improved FormData handling (@sophiebremer)
  • Removed HTON in favour of bridge system (@sophiebremer)

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.1.0...v0.2.0

v0.1.0

  • Added support of lists
  • Added support of scheduled status
  • Added support of HTON (Hyper-Text Object Notification)
  • Improved API
  • Improved JSON
  • Improved support of statuses
  • Simplified OAuth functions
  • Simplified Tooling

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.6...v0.1.0

v0.0.6

  • Add API.getMediaAttachment
  • Add API.postNewMediaAttachment
  • Add API.postNewPollVote
  • Add JSON.AudioMediaAttachment type
  • Add JSON.GIFVMediaAttachment type
  • Add JSON.NewMediaAttachment type
  • Add JSON.NewPollVote type
  • Add JSON.VideoMediaAttachment type
  • Fixed JSON.ImageMediaAttachment type
  • Fixed JSON.Poll type

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.5...v0.0.6

v0.0.5

  • Add test setup
  • Improve error handling

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.4...v0.0.5

v0.0.4

  • Fix new status validation

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.3...v0.0.4

v0.0.3

  • Fix Blob reference issue

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.2...v0.0.3

v0.0.2

  • Split Mastodon logic into API, JSON, REST
  • Add search API

Full Changelog: https://github.com/typescriptlibs/tsl-mastodon-api/compare/v0.0.1...v0.0.2

v0.0.1

  • Initial release
  • Add MastodonAPI.fetch
  • Add ES module structure
  • Add Promise support
  • Add TypeScript support