Forem API for .NET
API interface for Forem apps. Forem is the platform which powers DEV and other online communities.
API Support
We attempt to support all the API endpoints per the Forem/DEV API, which includes:
- Articles
- Users
- Podcasts
- Videos
Where available, the library supports pagination and page size controls. If there is an API endpoint that isn't currently supported, feel free to open an issue or create a PR.
Getting Started
Installation
To install the library run the below on Nuget Manager Console:
Install-Package Forem.Api -Version 0.4.0
Usage
-
For Some of the methods, DEV API key
apiKey
is required to be passed to authenticate you as a user. To obtain one, check the authentication section GET API KEY -
Using DI, Add the service to the container just by doing the below:
services.AddForemApi(new Uri("https://dev.to/"));
-
Configure your
HttpClient
DI by adding the below snippet to yourstartup.cs
file (or wherever you're configuring your DI things):services.AddHttpClient(); services.AddTransient(provider => { return provider.GetRequiredService<IHttpClientFactory>().CreateClient(string.Empty); });
Having all this setup, then you're good to go!!
Snippets
Articles
using Forem.Api;
[Route("api/[controller]")]
[ApiController]
public class ArticleController : ControllerBase
{
private readonly IArticlesService _articlesService;
private readonly ITagsService _tagsService;
//Without Dependency Injection - global
ArticlesService articlesService = new ArticlesService(new Uri("https://dev.to/"), new HttpClient());
//With Dependency Injection
public ArticleController(IArticlesService articlesService, ITagsService tagsService)
{
_articlesService = articlesService;
_tagsService = tagsService;
}
To get all articles you simply just call GetArticlesAsync
passing optional params page
and perPage
with default values 1
& 30
respectively.
[HttpGet("all")]
public async Task<IActionResult> GetAllArticles(int page, int itemPerPage)
{
//Without DI - local
//var articlesService = new ArticlesService(new Uri("https://dev.to/"), new HttpClient());
var articles = await articlesService.GetArticlesAsync();
articles = await _articlesService.GetArticlesAsync(page, itemPerPage);
return Ok(articles);
}
Feel free to check out other methods/endpoints Articles Doc