Installation and Setup

  1. Install the Go library:
$go get github.com/SchematicHQ/schematic-go
  1. Issue an API key for the appropriate environment using the Schematic app. Be sure to capture the secret key when you issue the API key; you’ll only see this key once, and this is what you’ll use with the Schematic Go library.

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

1import (
2 "os"
3
4 "github.com/SchematicHQ/schematic-go"
5)
6
7func main() {
8 apiKey := os.Getenv("SCHEMATIC_API_KEY")
9 client, err := schematic.NewClient(apiKey)
10 defer client.Close()
11}

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 bytes) and the max age of the cache (in seconds):

1import (
2 "os"
3 "time"
4
5 "github.com/SchematicHQ/schematic-go"
6)
7
8func main() {
9 apiKey := os.Getenv("SCHEMATIC_API_KEY")
10 cacheSizeBytes := 1000000
11 cacheTTL := 1 * time.Second
12 client, err := schematic.NewClient(apiKey, schematic.WithLocalFlagCheckCache(cacheSizeBytes, cacheTTL))
13 defer client.Close()
14}

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

1import (
2 "os"
3
4 "github.com/SchematicHQ/schematic-go"
5)
6
7func main() {
8 apiKey := os.Getenv("SCHEMATIC_API_KEY")
9 client, err := schematic.NewClient(apiKey, schematic.WithDisabledFlagCheckCache())
10 defer client.Close()
11}

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). You can do this using an initialization option:

1import (
2 "os"
3
4 "github.com/SchematicHQ/schematic-go"
5)
6
7func main() {
8 apiKey := os.Getenv("SCHEMATIC_API_KEY")
9 client, err := schematic.NewClient(apiKey, schematic.WithDefaultFlagValues(map[string]bool{
10 "some-flag-key": true,
11 }))
12 defer client.Close()
13}

Usage examples

Sending identify events

Create or update users and companies using identify events.

1func main() {
2 client, err := schematic.NewClient(os.Getenv("SCHEMATIC_API_KEY"))
3 defer client.Close()
4
5 eventBody := NewEventBodyIdentify(map[string]any{
6 "email": "wcoyote@acme.net",
7 "user-id": "your-user-id",
8 })
9 eventBody.SetCompany(map[string]any{
10 "id": "your-company-id",
11 })
12 eventBody.SetName("Wile E. Coyote")
13 eventBody.SetTraits(map[string]any{
14 "city": "Atlanta",
15 "login_count": 24,
16 "is_staff": false,
17 })
18
19 client.Identify(context.Background(), eventBody)
20}

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.

1func main() {
2 client, err := schematic.NewClient(os.Getenv("SCHEMATIC_API_KEY"))
3 defer client.Close()
4
5 eventBody := NewEventBodyTrack("some-action")
6 eventBody.SetUser(map[string]any{
7 "email": "wcoyote@acme.net",
8 "user-id": "your-user-id",
9 })
10 eventBody.SetCompany(map[string]any{
11 "id": "your-company-id",
12 })
13
14 client.Track(context.Background(), eventBody)
15}

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:

1func main() {
2 client, err := schematic.NewClient(os.Getenv("SCHEMATIC_API_KEY"))
3 defer client.Close()
4
5 body := schematic.NewUpsertCompanyRequestBody(map[string]any{
6 "id": "your-company-id",
7 })
8 body.SetName("Acme Widgets, Inc.")
9 body.SetTraits(map[string]any{
10 "city": "Atlanta",
11 "high_score": 25,
12 "is_active": true,
13 })
14
15 resp, r, err := client.API().CompaniesAPI.CreateCompany(context.Background()).UpsertCompanyRequestBody(*body).Execute()
16}

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:

1func main() {
2 client, err := schematic.NewClient(os.Getenv("SCHEMATIC_API_KEY"))
3 defer client.Close()
4
5 companyKeys := map[string]any{
6 "id": "your-company-id",
7 }
8 userKeys := map[string]any{
9 "email": "wcoyote@acme.net",
10 "user-id": "your-user-id",
11 }
12 body := schematic.NewUpsertUserRequestBody(companyKeys, userKeys)
13 body.SetName("Wile E. Coyote")
14 body.SetTraits(map[string]any{
15 "city": "Atlanta",
16 "login_count": 24,
17 "is_staff": false,
18 })
19
20 resp, r, err := client.API().CompaniesAPI.CreateUser(context.Background()).UpsertUserRequestBody(*body).Execute()
21}

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.

1func main() {
2 client, err := schematic.NewClient(os.Getenv("SCHEMATIC_API_KEY"))
3 defer client.Close()
4
5 evaluationCtx := schematic.CheckFlagRequestBody{
6 Company: map[string]any{
7 "id": "your-company-id",
8 },
9 User: map[string]any{
10 "email": "wcoyote@acme.net",
11 "user-id": "your-user-id",
12 },
13 }
14
15 if client.CheckFlag(context.Background(), "some-flag-key", evaluationCtx) {
16 // Flag is on
17 } else {
18 // Flag is off
19 }
20}

Other API operations

The Schematic API supports many operations beyond these, accessible via client.API(). See the API submodule readme for a full list and documentation of supported operations.

Testing

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 providing an empty API key to the client:

1func main() {
2 client, err := schematic.NewClient("")
3 defer client.Close()
4}

Offline mode works well with flag defaults:

1func main() {
2 client, err := schematic.NewClient("", schematic.WithDefaultFlagValues(map[string]bool{
3 "some-flag-key": true,
4 }))
5 defer client.Close()
6}

In an automated testing context, you may also want to use offline mode and specify single flag responses for test cases:

1func TestSomeFunctionality(t *testing.T) {
2 client, err := schematic.NewClient("")
3 defer client.Close()
4
5 client.SetFlagDefault("some-flag-key", true)
6
7 // test code that expects the flag to be on
8}

Mocks

If you prefer, you can also use mocks:

1import (
2 "testing"
3
4 "go.uber.org/mock/gomock"
5 schematicmocks "github.com/SchematicHQ/schematic-go/mocks"
6)
7
8
9func TestSomeFunctionality(t *testing.T) {
10 ctrl := gomock.NewController(t)
11 schematic := schematicmocks.NewMockClient(ctrl)
12 client.EXPECT().CheckFlag(context.Background(), gomock.Any(), "some-flag-key").Return(true)
13
14 // test code that expects the flag to be on
15}