Authorizinganapplication
Although not all of the API methods require authentication to be used, most of them do. The
authentication process involves a) registering an application with a Mastodon server to obtain a client
secret and ID; b) authorizing the application by either providing a user's credentials, or by using an
authentication URL.
The methods facilitating this process are detailed below:
register()register(%data)
Obtain a client secret and ID from a given mastodon instance. Takes a single hash as an argument,
with the following possible keys:
redirect_uris
The URL to which authorization codes should be forwarded after authorized by the user. Defaults
to the value of the redirect_uri attribute.
scopes
The scopes requested by this client. Defaults to the value of the scopes attribute.
website
The client's website. Defaults to the value of the "website" attribute.
When successful, sets the "client_secret", "scopes", and "client_id" attributes of the
Mastodon::Client object and returns the modified object.
This should be called once per client and its contents cached locally.
authorization_url()
Generate an authorization URL for the given application. Accessing this URL via a browser by a logged
in user will allow that user to grant this application access to the requested scopes. The scopes
used are the ones in the scopes attribute at the time this method is called.
authorize()authorize(%data)
Grant the application access to the requested scopes for a given user. This method takes a hash with
either an access code or a user's login credentials to grant authorization. Valid keys are:
access_code
The access code obtained by visiting the URL generated by authorization_url.
usernamepassword
The user's login credentials.
When successful, the method automatically sets the client's authorized attribute to a true value and
caches the access_token for all future calls.
Errorhandling
Methods that make requests to the server will "die" whenever an error is encountered, or the data that
was received from the server is not what is expected. The error string will, when possible, come from the
response's status line, but this will likely not be enough to fully diagnose what went wrong.
latest_response
To make this easier, the client will cache the server's response after each request has been made,
and expose it through the "latest_response" accessor.
Note that, as its name implies, thiswillonlystorethemostrecentresponse.
If called before any request has been made, it will return an undefined value.
The remaining methods listed here follow the order of those in the official API documentation.
Accountsget_account()get_account($id)get_account($params)get_account($id,$params)
Fetches an account by ID. If no ID is provided, this defaults to the current authenticated account.
Global GET parameters are available for this method.
Depending on the value of "coerce_entities", it returns a Mastodon::Entity::Account object, or a
plain hash reference.
update_account($params)
Make changes to the authenticated account. Takes a hash reference with the following possible keys:
display_namenote
Strings
avatarheader
A base64 encoded image, or the name of a file to be encoded.
Depending on the value of "coerce_entities", returns the modified Mastodon::Entity::Account object,
or a plain hash reference.
followers()followers($id)followers($params)followers($id,$params)
Get the list of followers of an account by ID. If no ID is provided, the one for the current
authenticated account is used. Global GET parameters are available for this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
following()following($id)following($params)following($id,$params)
Get the list of accounts followed by the account specified by ID. If no ID is provided, the one for
the current authenticated account is used. Global GET parameters are available for this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
statuses()statuses($id)statuses($params)statuses($id,$params)
Get a list of statuses from the account specified by ID. If no ID is provided, the one for the
current authenticated account is used.
In addition to the global GET parameters, this method accepts the following parameters:
only_mediaexclude_replies
Both boolean.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Status
objects, or a plain array reference.
follow($id)unfollow($id)
Follow or unfollow an account specified by ID. The ID argument is mandatory.
Depending on the value of "coerce_entities", returns the new Mastodon::Entity::Relationship object,
or a plain hash reference.
block($id)unblock($id)
Block or unblock an account specified by ID. The ID argument is mandatory.
Depending on the value of "coerce_entities", returns the new Mastodon::Entity::Relationship object,
or a plain hash reference.
mute($id)unmute($id)
Mute or unmute an account specified by ID. The ID argument is mandatory.
Depending on the value of "coerce_entities", returns the new Mastodon::Entity::Relationship object,
or a plain hash reference.
relationships(@ids)relationships(@ids,$params)
Get the list of relationships of the current authenticated user with the accounts specified by ID. At
least one ID is required, but more can be passed at once. Global GET parameters are available for
this method, and can be passed as an additional hash reference as a final argument.
Depending on the value of "coerce_entities", returns an array reference of
Mastodon::Entity::Relationship objects, or a plain array reference.
search_accounts($query)search_accounts($query,$params)
Search for accounts. Takes a mandatory string argument to use as the search query. If the search
query is of the form "username@domain", the accounts will be searched remotely.
In addition to the global GET parameters, this method accepts the following parameters:
limit
The maximum number of matches. Defaults to 40.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
This method does not require authentication.
Blocksblocks()blocks($params)
Get the list of accounts blocked by the authenticated user. Global GET parameters are available for
this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
Favouritesfavourites()favourites($params)
Get the list of statuses favourited by the authenticated user. Global GET parameters are available
for this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Status
objects, or a plain array reference.
Followrequestsfollow_requests()follow_requests($params)
Get the list of accounts requesting to follow the the authenticated user. Global GET parameters are
available for this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
authorize_follow($id)reject_follow($id)
Accept or reject the follow request by the account of the specified ID. The ID argument is mandatory.
Returns an empty object.
Followsremote_follow($acct)
Follow a remote user by account string (ie. "username@domain"). The argument is mandatory.
Depending on the value of "coerce_entities", returns an Mastodon::Entity::Account object, or a plain
hash reference with the local representation of the specified account.
Instancesfetch_instance()
Fetches the latest information for the current instance the client is talking to. When successful,
this method updates the value of the "instance" attribute.
Depending on the value of "coerce_entities", returns an Mastodon::Entity::Instance object, or a plain
hash reference.
This method does not require authentication.
Mediaupload_media($file)upload_media($file,$params)
Upload a file as an attachment. Takes a mandatory argument with the name of a local file to encode
and upload, and an optional hash reference with the following additional parameters:
description
A plain-text description of the media, for accessibility, as a string.
focus
An array reference of two floating point values, to be used as the x and y focus values. These
inform clients which point in the image is the most important one to show in a cropped view.
The value of a coordinate is a real number between -1 and +1, where 0 is the center. x:-1
indicates the left edge of the image, x:1 the right edge. For the y axis, y:1 is the top edge and
y:-1 is the bottom.
Depending on the value of "coerce_entities", returns an Mastodon::Entity::Attachment object, or a
plain hash reference.
The returned object's ID can be passed to the post_status to post it to a timeline.
Mutesmutes()mutes($params)
Get the list of accounts muted by the authenticated user. Global GET parameters are available for
this method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Account
objects, or a plain array reference.
Notificationsnotifications()notifications($params)
Get the list of notifications for the authenticated user. Global GET parameters are available for
this method.
Depending on the value of "coerce_entities", returns an array reference of
Mastodon::Entity::Notification objects, or a plain array reference.
get_notification($id)
Get a notification by ID. The argument is mandatory.
Depending on the value of "coerce_entities", returns an Mastodon::Entity::Notification object, or a
plain hash reference.
clear_notifications()
Clears all notifications for the authenticated user.
This method takes no arguments and returns an empty object.
Reportsreports()reports($params)
Get a list of reports made by the authenticated user. Global GET parameters are available for this
method.
Depending on the value of "coerce_entities", returns an array reference of Mastodon::Entity::Report
objects, or a plain array reference.
report($params)
Report a user or status. Takes a mandatory hash with the following keys:
account_id
The ID of a single account to report.
status_ids
The ID of a single status to report, or an array reference of statuses to report.
comment
An optional string.
While the comment is always optional, either the account_id or the list of status_ids must be
present.
Depending on the value of "coerce_entities", returns the new Mastodon::Entity::Report object, or a
plain hash reference.
Searchsearch($query)search($query,$params)
Search for content. Takes a mandatory string argument to use as the search query. If the search query
is a URL, Mastodon will attempt to fetch the provided account or status. Otherwise, it will do a
local account and hashtag search.
In addition to the global GET parameters, this method accepts the following parameters:
resolve
Whether to resolve non-local accounts.
Statusesget_status($id)get_status($id,$params)
Fetches a status by ID. The ID argument is mandatory. Global GET parameters are available for this
method as an additional hash reference.
Depending on the value of "coerce_entities", it returns a Mastodon::Entity::Status object, or a plain
hash reference.
This method does not require authentication.
get_status_context($id)get_status_context($id,$params)
Fetches the context of a status by ID. The ID argument is mandatory. Global GET parameters are
available for this method as an additional hash reference.
Depending on the value of "coerce_entities", it returns a Mastodon::Entity::Context object, or a
plain hash reference.
This method does not require authentication.
get_status_card($id)get_status_card($id,$params)
Fetches a card associated to a status by ID. The ID argument is mandatory. Global GET parameters are
available for this method as an additional hash reference.
Depending on the value of "coerce_entities", it returns a Mastodon::Entity::Card object, or a plain
hash reference.
This method does not require authentication.
get_status_reblogs($id)get_status_reblogs($id,$params)get_status_favourites($id)get_status_favourites($id,$params)
Fetches a list of accounts who have reblogged or favourited a status by ID. The ID argument is
mandatory. Global GET parameters are available for this method as an additional hash reference.
Depending on the value of "coerce_entities", it returns an array reference of
Mastodon::Entity::Account objects, or a plain array reference.
This method does not require authentication.
post_status($text)post_status($text,$params)
Posts a new status. Takes a mandatory string as the content of the status (which can be the empty
string), and an optional hash reference with the following additional parameters:
status
The content of the status, as a string. Since this is already provided as the first argument of
the method, this is not necessary. But if provided, this value will overwrite that of the first
argument.
in_reply_to_id
The optional ID of a status to reply to.
media_ids
An array reference of up to four media IDs. These can be obtained as the result of a call to
upload_media().
sensitive
Boolean, to mark status content as NSFW.
spoiler_text
A string, to be shown as a warning before the actual content.
visibility
A string; one of "direct", "private", "unlisted", or "public".
Depending on the value of "coerce_entities", it returns the new Mastodon::Entity::Status object, or a
plain hash reference.
delete_status($id)
Delete a status by ID. The ID is mandatory. Returns an empty object.
reblog($id)unreblog($id)favourite($id)unfavourite($id)
Reblog or favourite a status by ID, or revert this action. The ID argument is mandatory.
Depending on the value of "coerce_entities", it returns the specified Mastodon::Entity::Status
object, or a plain hash reference.
Timelinestimeline($query)timeline($query,$params)
Retrieves a timeline. The first argument defines either the name of a timeline (which can be one of
"home" or "public"), or a hashtag (if it begins with the "#" character). This argument is mandatory.
In addition to the global GET parameters, this method accepts the following parameters:
Accessing the public and tag timelines does not require authentication.
local
Boolean. If true, limits results only to those originating from the current instance. Only
applies to public and tag timelines.
Depending on the value of "coerce_entities", it returns an array of Mastodon::Entity::Status objects,
or a plain array reference. The more recent statuses come first.