https://github.com/abjerner/skybrud.social.mastodon
.NET API wrapper and implementation of the Mastodon API.
https://github.com/abjerner/skybrud.social.mastodon
api api-client api-wrapper csharp dotnet limbo mastodon mastodon-api package skybrud skybrud-integrations skybrud-social social
Last synced: 10 months ago
JSON representation
.NET API wrapper and implementation of the Mastodon API.
- Host: GitHub
- URL: https://github.com/abjerner/skybrud.social.mastodon
- Owner: abjerner
- License: mit
- Created: 2023-09-23T10:41:07.000Z (over 2 years ago)
- Default Branch: v1/main
- Last Pushed: 2024-03-30T15:05:08.000Z (about 2 years ago)
- Last Synced: 2025-07-22T06:31:42.509Z (11 months ago)
- Topics: api, api-client, api-wrapper, csharp, dotnet, limbo, mastodon, mastodon-api, package, skybrud, skybrud-integrations, skybrud-social, social
- Language: C#
- Homepage: https://packages.limbo.works/skybrud.social.mastodon/
- Size: 508 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Skybrud.Social.Mastodon
[](https://github.com/abjerner/Skybrud.Social.Mastodon/blob/v1/main/LICENSE.md)
[](https://www.nuget.org/packages/Skybrud.Social.Mastodon)
[](https://www.nuget.org/packages/Skybrud.Social.Mastodon)
[](https://packages.limbo.works/skybrud.social.mastodon/)
.NET API wrapper and implementation of the [**Mastodon API**](https://docs.joinmastodon.org/client/intro/).
License:
MIT License
Target Framework:
.NET Standard 2.0, .NET 6 and .NET 7
## Installation
The package is only available via [**NuGet**](https://www.nuget.org/packages/Skybrud.Social.Mastodon/1.0.0-alpha005). To install the package, you can either use the .NET CLI:
```
dotnet add package Skybrud.Social.Mastodon --version 1.0.0-alpha005
```
or the NuGet Package Manager:
```
Install-Package Skybrud.Social.Mastodon -Version 1.0.0-alpha005
```
## Examples
### Getting a public timeline
The example below shows how to get the 40 most recent status from the public timeline of the [**umbracocommunity.social**](https://umbracocommunity.social/) server.
By the default the API will return statuses from all Mastodon servers, but the `Local = true` will ensure that only statuses from the [**umbracocommunity.social**](https://umbracocommunity.social/) server are returned.
```cshtml
@using Skybrud.Social.Mastodon
@using Skybrud.Social.Mastodon.Options.Timeline
@using Skybrud.Social.Mastodon.Responses.Statuses
@{
// Initialize a new HTTP service (basically the API wrapper)
MastodonHttpService mastodon = MastodonHttpService.CreateFromDomain("umbracocommunity.social");
// Initialize the options for the request to the API
MastodonGetPublicTimelineOptions options = new() {
Limit = 40,
Local = true
};
// Make the request to the API
MastodonStatusListResponse response = await mastodon
.Timelines
.GetPublicTimelineAsync(options);
// Iterate through the first 40 statuses
foreach (var status in response.Body) {
@status.Account.DisplayName - @status.Content
}
}
```
### Getting a hashtag timeline
The example below shows how to get the 40 most recent status from the [#Umbraco](https://umbracocommunity.social/tags/Umbraco) hashtag.
Although this example uses the [**umbracocommunity.social**](https://umbracocommunity.social/), the returned status may also come from other Mastodon servers. To only return local statuses, you can add `Local = true` to the options.
```cshtml
@using Skybrud.Social.Mastodon
@using Skybrud.Social.Mastodon.Options.Timeline
@using Skybrud.Social.Mastodon.Responses.Statuses
@{
// Initialize a new HTTP service (basically the API wrapper)
MastodonHttpService mastodon = MastodonHttpService.CreateFromDomain("umbracocommunity.social");
// Initialize the options for the request to the API
MastodonGetHashtagTimelineOptions options = new() {
Hashtag = "umbraco",
Limit = 40,
//Local = true
};
// Make the request to the API
MastodonStatusListResponse response = await mastodon
.Timelines
.GetHashtagTimelineAsync(options);
// Iterate through the first 40 statuses
foreach (var status in response.Body) {
@status.CreatedAt - @status.Account.DisplayName - @status.Content
}
}
```
### Posting a new status
The example below creates a new `MastodonHttpService` instance from an access token, and then attempts to post two new statuses where the second is a reply to the first:
```cshtml
@using Skybrud.Social.Mastodon
@using Skybrud.Social.Mastodon.Exceptions
@using Skybrud.Social.Mastodon.Models.Statuses
@using Skybrud.Social.Mastodon.Options.Statuses
@using Skybrud.Social.Mastodon.Responses.Statuses
@{
// Initialize a new HTTP service (basically the API wrapper)
MastodonHttpService mastodon = MastodonHttpService
.CreateFromAccessToken("umbracocommunity.social", "Your access token");
First
MastodonStatus first;
try {
MastodonStatusResponse response = await mastodon.Statuses.PostStatusAsync(new MastodonPostStatusOptions {
Status = "Hello world! #test"
});
first = response.Body;
@first.JObject
} catch (MastodonHttpException ex) {
@ex
@ex.Error
@ex.Response.Body
return;
} catch (Exception ex) {
@ex
return;
}
Second
try {
MastodonStatusResponse response = await mastodon.Statuses.PostStatusAsync(new MastodonPostStatusOptions {
Status = "Hej verden! #test",
InReplyTo = first.Id
});
var second = response.Body;
@second.JObject
} catch (MastodonHttpException ex) {
@ex
@ex.Error
@ex.Response.Body
return;
} catch (Exception ex) {
@ex
return;
}
}
```