The official Schematic C# library, supporting .NET Standard, .NET Core, and .NET Framework.

Installation and Setup

  1. Install the library using the .NET Core command-line interface (CLI) tools:
$dotnet add package SchematicHQ.Client

or using the NuGet Command Line Interface (CLI):

$nuget install SchematicHQ.Client
  1. Issue an API key for the appropriate environment using the Schematic app.

  2. Using this secret key, initialize a client in your application:

1using SchematicHQ;
2
3Schematic schematic = new Schematic("YOUR_API_KEY")

Usage

Sending identify events

Create or update users and companies using identify events.

1using SchematicHQ.Client;
2using System.Collections.Generic;
3using OneOf;
4
5Schematic schematic = new Schematic("YOUR_API_KEY");
6
7schematic.Identify(
8 keys: new Dictionary<string, string>
9 {
10 { "email", "wcoyote@acme.net" },
11 { "user_id", "your-user-id" }
12 },
13 company: new EventBodyIdentifyCompany
14 {
15 Keys = new Dictionary<string, string> { { "id", "your-company-id" } },
16 Name = "Acme Widgets, Inc.",
17 Traits = new Dictionary<string, OneOf<string, double, bool, OneOf<string, double, bool>>>
18 {
19 { "city", "Atlanta" }
20 }
21 },
22 name: "Wile E. Coyote",
23 traits: new Dictionary<string, OneOf<string, double, bool, OneOf<string, double, bool>>>
24 {
25 { "login_count", 24 },
26 { "is_staff", false }
27 }
28);

This call is non-blocking and there is no response to check.

Sending track events

Track activity in your application using track events; these events can later be used to produce metrics for targeting.

1Schematic schematic = new Schematic("YOUR_API_KEY");
2schematic.Track(
3 eventName: "some-action",
4 user: new Dictionary<string, string> { { "user_id", "your-user-id" } },
5 company: new Dictionary<string, string> { { "id", "your-company-id" } }
6);

This call is non-blocking and there is no response to check.

Creating and updating companies

Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation:

1using SchematicHQ.Client;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4
5Schematic schematic = new Schematic("YOUR_API_KEY");
6
7// Creating and updating companies
8async Task UpsertCompanyExample()
9{
10 var response = await schematic.API.Companies.UpsertCompanyAsync(new UpsertCompanyRequestBody
11 {
12 Keys = new Dictionary<string, string> { { "id", "your-company-id" } },
13 Name = "Acme Widgets, Inc.",
14 Traits = new Dictionary<string, object>
15 {
16 { "city", "Atlanta" },
17 { "high_score", 25 },
18 { "is_active", true }
19 }
20 });
21
22 // Handle the response as needed
23 Console.WriteLine($"Company upserted: {response.Data.Name}");
24}

You can define any number of company keys; these are used to address the company in the future, for example by updating the company’s traits or checking a flag for the company.

You can also define any number of company traits; these can then be used as targeting parameters.

Creating and updating users

Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation:

1using SchematicHQ.Client;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4
5Schematic schematic = new Schematic("YOUR_API_KEY");
6
7// Creating and updating users
8async Task UpsertUserExample()
9{
10 var response = await schematic.API.Companies.UpsertUserAsync(new UpsertUserRequestBody
11 {
12 Keys = new Dictionary<string, string>
13 {
14 { "email", "wcoyote@acme.net" },
15 { "user_id", "your-user-id" }
16 },
17 Name = "Wile E. Coyote",
18 Traits = new Dictionary<string, object>
19 {
20 { "city", "Atlanta" },
21 { "high_score", 25 },
22 { "is_active", true }
23 },
24 Company = new Dictionary<string, string> { { "id", "your-company-id" } }
25 });
26
27 // Handle the response as needed
28 Console.WriteLine($"User upserted: {response.Data.Name}");
29}

You can define any number of user keys; these are used to address the user in the future, for example by updating the user’s traits or checking a flag for the user.

You can also define any number of user traits; these can then be used as targeting parameters.

Checking flags

When checking a flag, you’ll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you’ll get the default value for the flag.

1Schematic schematic = new Schematic("YOUR_API_KEY");
2
3bool flagValue = await schematic.CheckFlag(
4 "some-flag-key",
5 company: new Dictionary<string, string> { { "id", "your-company-id" } },
6 user: new Dictionary<string, string> { { "user_id", "your-user-id" } }
7);

Configuration Options

There are a number of configuration options that can be specified passing in ClientOptions as a second parameter when instantiating the Schematic client.

Flag Check Options

By default, the client will do some local caching for flag checks. If you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of cached items) and the max age of the cache:

1using SchematicHQ.Client;
2using System.Collections.Generic;
3
4int cacheMaxItems = 1000; // Max number of entries in the cache
5TimeSpan cacheTtl = TimeSpan.FromSeconds(1); // Set TTL to 1 second
6
7var options = new ClientOptions
8{
9 CacheProviders = new List<ICacheProvider<bool?>>
10 {
11 new LocalCache<bool?>(cacheMaxItems, cacheTtl)
12 }
13};
14
15Schematic schematic = new Schematic("YOUR_API_KEY", options);

Note about LocalCache: LocalCache implementation returns default value the type it is initiated with when it is a cache miss. Hence, when using with Schematic it is initiated with type (bool?) so that cache returns null when it is a miss

You can also disable local caching entirely; bear in mind that, in this case, every flag check will result in a network request:

1using SchematicHQ.Client;
2using System.Collections.Generic;
3
4var options = new ClientOptions
5{
6 CacheProviders = new List<ICacheProvider<bool?>>()
7};
8
9Schematic schematic = new Schematic("YOUR_API_KEY", options);

You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below):

1using SchematicHQ.Client;
2using System.Collections.Generic;
3
4var options = new ClientOptions
5{
6 FlagDefaults = new Dictionary<string, bool>
7 {
8 { "some-flag-key", true }
9 }
10};
11
12Schematic schematic = new Schematic("YOUR_API_KEY", options);

Offline Mode

In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by specifying the Offline option; in this case, it does not matter what API key you specify:

1using SchematicHQ.Client;
2
3var options = new ClientOptions
4{
5 Offline = true
6};
7
8Schematic schematic = new Schematic("", options); // API key doesn't matter in offline mode

Offline mode works well with flag defaults:

1using SchematicHQ.Client;
2using System.Collections.Generic;
3
4var options = new ClientOptions
5{
6 FlagDefaults = new Dictionary<string, bool>
7 {
8 { "some-flag-key", true }
9 },
10 Offline = true
11};
12
13Schematic schematic = new Schematic("", options);
14
15bool flagValue = await schematic.CheckFlag("some-flag-key"); // Returns true

Event Buffer

Schematic API uses an Event Buffer to batch Identify and Track requests and avoid multiple API calls. You can set the event buffer flush period in options:

1using SchematicHQ.Client;
2
3var options = new ClientOptions
4{
5 DefaultEventBufferPeriod = TimeSpan.FromSeconds(5)
6};

You may also want to use your custom event buffer. To do so, your custom event buffer has to implement IEventBuffer interface, and pass an instance to the Schematic API through options:

1using SchematicHQ.Client;
2
3var options = new ClientOptions
4{
5 EventBuffer = new MyCustomEventBuffer();//instance of your custom event buffer
6}

HTTP Client

You can override the HttpClient:

1schematic = new Schematic("YOUR_API_KEY", new ClientOptions{
2 HttpClient = ... // Override the Http Client
3 BaseURL = ... // Override the Base URL
4})

Retries

429 Rate Limit, and >=500 Internal errors will all be retried twice with exponential backoff. You can override this behavior globally or per-request.

1var schematic = new Schematic("...", new ClientOptions{
2 MaxRetries = 1 // Only retry once
3});

Timeouts

The SDK defaults to a 60s timeout. You can override this behaviour globally or per-request.

1var schematic = new Schematic("...", new ClientOptions{
2 TimeoutInSeconds = 20 // Lower timeout
3});

Exception Handling

When the API returns a non-zero status code, (4xx or 5xx response), a subclass of SchematicException will be thrown:

1using SchematicHQ;
2
3try {
4 schematic.Accounts.ListApiKeysAsync(...);
5} catch (SchematicException e) {
6 System.Console.WriteLine(e.Message)
7 System.Console.WriteLine(e.StatusCode)
8}