Skip to content

strapi-extensions/strapi-csharp-sdk

Repository files navigation

Strapi CSharp SDK

publish status on main Nuget License

Documenations

For more details please go to

https://strapi-extensions.github.io/strapi-csharp-sdk/

Installation

dotnet add package BetterCoding.Strapi.SDK.Core

more installation details can be found here.

Usage

the following section shows how config your servers in your server side .NET project, something like ASP.NET Core Web API, it is not recommended to configure the api token directly on the client.

Config servers - Server Side

var serverConfiguration = new StrapiServerConfiguration()
{
    APIToken = "your api token for this server",
    ServerURI = "http://localhost:1337",
    Alias = "development",
    IsDefault = true,
};

StrapiClient.AddServer(serverConfiguration);

if you want to use multiple servers you can config them like this

var development = new StrapiServerConfiguration()
{
    APIToken = "your api token for this server",
    ServerURI = "http://localhost:1337",
    Alias = "development",
    IsDefault = true,
};

var stage = new StrapiServerConfiguration()
{
    APIToken = "your api token for this server",
    ServerURI = "http://your-stage-server.com",
    Alias = "stage",
    IsDefault = false,
};

var production = new StrapiServerConfiguration()
{
    APIToken = "your api token for this server",
    ServerURI = "http://your-production-server.com",
    Alias = "production",
    IsDefault = false,
};

StrapiClient.AddServers(development, stage, production);

Query

Get entry by id

var todo = await StrapiClient.GetClient()
    .GetFiltersBuilder()
    .EntryName("todo")
    .PluralApiId("todos")
    .GetAsync(1);

Filter entries

var todos = await StrapiClient.GetClient()
    .GetQueryBuilder()
    .EntryName("todo")
    .PluralApiId("todos")
    .DeepEqualsTo("whoCreated", "id", userId)
    .FindAsync();

//model mapping
return todos.Select(t => new TodoEntry
{
    Id = t.Id,
    Due = t.Get<DateTime>("due"),
    Title = t.Get<string>("title"),
    Completed = t.Get<bool>("completed"),
}).ToList();

if you are using multiple-servers mode, you can switch server when calling GetClient by passing the alias of server:

var todos = await StrapiClient.GetClient("stage")
    .GetQueryBuilder()
    .EntryName("todo")
    .PluralApiId("todos")
    .DeepEqualsTo("whoCreated", "id", userId)
    .FindAsync();

User

Log in with local auth

await StrapiClient.GetClient()
    .GetAuthBuilder()
    .Identifier("your-username")
    .Password("your-password")
    .LogInAsync();