supabase-csharp icon indicating copy to clipboard operation
supabase-csharp copied to clipboard

A C# Client library for Supabase

Stage: Beta


Integrate your Supabase projects with C#.

Includes C# features to make supabase function more like an ORM - specifically the ability to leverage strongly typed models.

API is heavily modeled after the supabase-js repo and postgrest-js repo.

Status

Getting Started

Care has been taken to make API interactions mirror - as much as possible - the Javascript API. However, there are some places where Supabase-csharp deviates to make use of C# goodies that Javascript doesn't have.

Getting started is pretty easy!

Grab your API URL and Public Key from the Supabase admin panel.

public async void Main()
{
  // Make sure you set these (or similar)
  var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
  var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");

  await Supabase.Client.InitializeAsync(url, key);
  // That's it - forreal. Crazy right?

  // The Supabase Instance can be accessed at any time using:
  //  Supabase.Client.Instance {.Realtime|.Auth|etc.}
  // For ease of readability we'll use this:
  var instance = Supabase.Client.Instance;

  // Access Postgrest using:
  var channels = await instance.From<Channel>().Get();

  // Access Auth using:
  await instance.Auth.SignIn(email, password);
  Debug.WriteLine(instance.Auth.CurrentUser.Id);

  // Interested in Realtime Events?
  var table = await instance.From<Channel>();
  table.On(ChannelEventType.Insert, Channel_Inserted);
  table.On(ChannelEventType.Delete, Channel_Deleted);
  table.On(ChannelEventType.Update, Channel_Updated);

  // Invoke an Edge Function
  var result = await instance.Functions.Invoke("hello", new Dictionary<string, object> { 
      { "name", "Ronald" } 
  });

  // Run a Remote Stored Procedure:
  await instance.Rpc("my_cool_procedure", params);

  // Interact with Supabase Storage
  var storage = Supabase.Client.Instance.Storage
  await storage.CreateBucket("testing")

  var bucket = storage.From("testing");

  var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:", "");
  var imagePath = Path.Combine(basePath, "Assets", "supabase-csharp.png");

  await bucket.Upload(imagePath, "supabase-csharp.png");

  // If bucket is public, get url
  bucket.GetPublicUrl("supabase-csharp.png"));

  // If bucket is private, generate url
  await bucket.CreateSignedUrl("supabase-csharp.png", 3600));

  // Download it!
  await bucket.Download("supabase-csharp.png", Path.Combine(basePath, "testing-download.png"));
}

Models:

Supabase-csharp is heavily dependent on Models deriving from SupabaseModel (which derive from Postgrest-chsharp's BaseModel). To interact with the API, one must have the associated model specified.

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

[Table("messages")]
public class Message : SupabaseModel
{
    // `ShouldInsert` Set to false so-as to honor DB generated key
    // If the primary key was set by the application, this could be omitted.
    [PrimaryKey("id", false)]
    public int Id { get; set; }

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

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

Package made possible through the efforts of:

Join the ranks! See a problem? Help fix it!

Made with contrib.rocks.

Contributing

We are more than happy to have contributions! Please submit a PR.