For AI agents: the documentation index is at llms.txt. Guide pages have a markdown version at the same path with .md appended, before any query string or fragment.

C#: Initializing

Initializing a new client is pretty straightforward. Find your project url and public key from the admin panel and pass it into your client initialization function.

Supabase is heavily dependent on Models deriving from BaseModel. To interact with the API, one must have the associated model (see example) specified.

Leverage Table, PrimaryKey, and Column attributes to specify names of classes/properties that are different from their C# Versions.

Examples

Standard

var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");

var options = new Supabase.SupabaseOptions
{
    AutoConnectRealtime = true
};

var supabase = new Supabase.Client(url, key, options);
await supabase.InitializeAsync();

ASP.NET Core: register the client

using Supabase.Core.Http;
using Supabase.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSupabase(
    supabaseUrl: builder.Configuration["Supabase:Url"]!,
    supabaseKey: builder.Configuration["Supabase:Key"]!,
    configureOptions: options =>
    {
        options.AutoConnectRealtime = true;
        options.PostgrestRetry = new RetryOptions { MaxRetries = 3 };
    });

var app = builder.Build();

ASP.NET Core: inject the client

app.MapGet("/todos", async (Supabase.Client supabase) =>
{
    var result = await supabase.From<Todo>().Get();
    return result.Models;
});

public class TodoService
{
    private readonly Supabase.Client _supabase;

    public TodoService(Supabase.Client supabase) => _supabase = supabase;

    public async Task<List<Todo>> GetOpen()
    {
        var result = await _supabase
            .From<Todo>()
            .Where(t => t.IsComplete == false)
            .Get();

        return result.Models;
    }
}

ASP.NET Core: inject a single sub-client

app.MapGet("/todos", async (IPostgrestClient postgrest) =>
    await postgrest.Table<Todo>().Get());

ASP.NET Core: initialize the client

app.MapGet("/todos", async (Supabase.Client supabase) =>
{
    await supabase.InitializeAsync();

    var result = await supabase.From<Todo>().Get();
    return result.Models;
});

Blazor

@page "/todos"
@using Supabase
@inject Client SupabaseClient

<ul>
    @foreach (var todo in _todos)
    {
        <li>@todo.Task</li>
    }
</ul>

@code {
    private List<Todo> _todos = new();

    protected override async Task OnInitializedAsync()
    {
        var result = await SupabaseClient.From<Todo>().Get();
        _todos = result.Models;
    }
}

Dependency Injection (Maui-like)

public static MauiApp CreateMauiApp()
{
      // ...
      var builder = MauiApp.CreateBuilder();

      var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
      var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");
      var options = new SupabaseOptions
      {
        AutoRefreshToken = true,
        AutoConnectRealtime = true,
        // SessionHandler = new SupabaseSessionHandler() <-- This must be implemented by the developer
      };

      // Note the creation as a singleton.
      builder.Services.AddSingleton(provider => new Supabase.Client(url, key, options));
}

With session persistence

var options = new Supabase.SupabaseOptions
{
    SessionHandler = new MySessionHandler() // Implements IGotrueSessionPersistence<Session>
};

var supabase = new Supabase.Client(url, key, options);
await supabase.InitializeAsync();

With the PKCE flow

var options = new Supabase.SupabaseOptions
{
    AuthFlowType = Supabase.Gotrue.Constants.OAuthFlowType.PKCE
};

var supabase = new Supabase.Client(url, key, options);

With logging

var options = new Supabase.SupabaseOptions
{
    LoggerFactory = loggerFactory
};

var supabase = new Supabase.Client(url, key, options);

With your own HttpClient

var options = new Supabase.SupabaseOptions
{
    HttpClient = new HttpClient(new MyLoggingHandler())
};

var supabase = new Supabase.Client(url, key, options);

Disable trace context propagation

var options = new Supabase.SupabaseOptions
{
    TracePropagation = Supabase.Core.Http.TracePropagation.Disabled
};

var supabase = new Supabase.Client(url, key, options);

With Models Example

// Given the following Model representing the Supabase Database (Message.cs)
[Table("messages")]
public class Message : BaseModel
{
    [PrimaryKey("id")]
    public int Id { get; set; }

    [Column("username")]
    public string UserName { get; set; }

    [Column("channel_id")]
    public int ChannelId { get; set; }

    public override bool Equals(object obj)
    {
        return obj is Message message &&
                Id == message.Id;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Id);
    }
}

// Get All Messages
var response = await supabase.From<Message>().Get();
List<Message> models = response.Models;

// Insert
var newMessage = new Message { UserName = "acupofjose", ChannelId = 1 };
await supabase.From<Message>().Insert(newMessage);

// Update
var model = response.Models.First();
model.UserName = "elrhomariyounes";
await model.Update<Message>();

// Delete
await response.Models.Last().Delete<Message>();

// etc.