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 option "github.com/schematichq/schematic-go/option"
5 schematicclient "github.com/schematichq/schematic-go/client"
6)
7
8func main() {
9 apiKey := os.Getenv("SCHEMATIC_API_KEY")
10 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
11 defer client.Close()
12}

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 records) and the max age of the cache (as a time.Duration):

1import (
2 "os"
3 "time"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7)
8
9func main() {
10 apiKey := os.Getenv("SCHEMATIC_API_KEY")
11 cacheSize := 100
12 cacheTTL := 1 * time.Millisecond
13 client := schematicclient.NewSchematicClient(
14 option.WithAPIKey(apiKey),
15 option.WithLocalFlagCheckCache(cacheSize, cacheTTL),
16 )
17 defer client.Close()
18}

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 option "github.com/schematichq/schematic-go/option"
5 schematicclient "github.com/schematichq/schematic-go/client"
6)
7
8func main() {
9 apiKey := os.Getenv("SCHEMATIC_API_KEY")
10 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey), option.WithDisabledFlagCheckCache())
11 defer client.Close()
12}

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 option "github.com/schematichq/schematic-go/option"
5 schematicclient "github.com/schematichq/schematic-go/client"
6)
7
8func main() {
9 apiKey := os.Getenv("SCHEMATIC_API_KEY")
10 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey), option.WithDefaultFlagValues(map[string]bool{
11 "some-flag-key": true,
12 }))
13 defer client.Close()
14}

Usage examples

Sending identify events

Create or update users and companies using identify events.

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7 schematicgo "github.com/schematichq/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 client.Identify(context.Background(), &schematicgo.EventBodyIdentify{
16 Event: "some-action",
17 Company: map[string]string{
18 "id": "your-company-id",
19 },
20 User: map[string]string{
21 "email": "wcoyote@acme.net",
22 "user-id": "your-user-id",
23 },
24 Name: "Wile E. Coyote",
25 Traits: map[string]any{
26 "city": "Atlanta",
27 "login_count": 24,
28 "is_staff": false,
29 },
30 })
31}

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.

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7 schematicgo "github.com/schematichq/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 client.Track(context.Background(), &schematicgo.EventBodyTrack{
16 Event: "some-action",
17 Company: map[string]string{
18 "id": "your-company-id",
19 },
20 User: map[string]string{
21 "email": "wcoyote@acme.net",
22 "user-id": "your-user-id",
23 },
24 })
25}

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

If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7 schematicgo "github.com/schematichq/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 client.Track(context.Background(), &schematicgo.EventBodyTrack{
16 Event: "query-tokens",
17 Company: map[string]stringh{
18 "id": "your-company-id",
19 },
20 User: map[string]string{
21 "email": "wcoyote@acme.net",
22 "user-id": "your-user-id",
23 },
24 Quantity: schematicgo.Int(1500),
25 })
26}

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:

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7 schematicgo "github.com/schematichq/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 body := &schematicgo.UpsertCompanyRequestBody{
16 Keys: map[string]string{
17 "id": "your-company-id",
18 },
19 Name: "Acme Widgets, Inc.",
20 Traits: map[string]any{
21 "city": "Atlanta",
22 "high_score": 25,
23 "is_active": true,
24 },
25 })
26 resp, err := client.API().Companies.UpsertCompany(context.Background, body)
27}

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:

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/SchematicHQ/schematic-go/client"
7 schematicgo "github.com/SchematicHQ/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 body := &schematicgo.UpsertUserRequestBody{
16 Keys: map[string]string{
17 "email": "wcoyote@acme.net",
18 "user-id": "your-user-id",
19 },
20 Company: map[string]string{
21 "id": "your-company-id",
22 },
23 Name: "Wile E. Coyote",
24 Traits: map[string]any{
25 "city": "Atlanta",
26 "login_count": 24,
27 "is_staff": false,
28 },
29 })
30
31 resp, err := client.API().Companies.UpsertUser(context.Background(), body)
32}

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.

1import (
2 "context"
3 "os"
4
5 option "github.com/schematichq/schematic-go/option"
6 schematicclient "github.com/schematichq/schematic-go/client"
7 schematicgo "github.com/schematichq/schematic-go"
8)
9
10func main() {
11 apiKey := os.Getenv("SCHEMATIC_API_KEY")
12 client := schematicclient.NewSchematicClient(option.WithAPIKey(apiKey))
13 defer client.Close()
14
15 evaluationCtx := schematicgo.CheckFlagRequestBody{
16 Company: map[string]string{
17 "id": "your-company-id",
18 },
19 User: map[string]string{
20 "email": "wcoyote@acme.net",
21 "user-id": "your-user-id",
22 },
23 }
24
25 if client.CheckFlag(context.Background(), "some-flag-key", evaluationCtx) {
26 // Flag is on
27 } else {
28 // Flag is off
29 }
30}

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

1import (
2 schematicclient "github.com/schematichq/schematic-go/client"
3)
4
5func main() {
6 client := schematicclient.NewSchematicClient()
7 defer client.Close()
8}

You can also enable offline mode by providing an empty API key:

1import (
2 option "github.com/schematichq/schematic-go/option"
3 schematicclient "github.com/schematichq/schematic-go/client"
4)
5
6func main() {
7 client := schematicclient.NewSchematicClient(option.WithAPIKey(""))
8 defer client.Close()
9}

Or, by using the offline mode option:

1import (
2 option "github.com/schematichq/schematic-go/option"
3 schematicclient "github.com/schematichq/schematic-go/client"
4)
5
6func main() {
7 client := schematicclient.NewSchematicClient(option.WithOfflineMode())
8 defer client.Close()
9}

Offline mode works well with flag defaults:

1import (
2 option "github.com/schematichq/schematic-go/option"
3 schematicclient "github.com/schematichq/schematic-go/client"
4)
5
6func main() {
7 client := schematicclient.NewSchematicClient(option.WithOfflineMode(), option.WithDefaultFlagValues(map[string]bool{
8 "some-flag-key": true,
9 }))
10 defer client.Close()
11}

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

1import (
2 option "github.com/schematichq/schematic-go/option"
3 schematicclient "github.com/schematichq/schematic-go/client"
4)
5
6func TestSomeFunctionality(t *testing.T) {
7 client := schematicclient.NewSchematicClient(option.WithOfflineMode())
8 defer client.Close()
9
10 client.SetFlagDefault("some-flag-key", true)
11
12 // test code that expects the flag to be on
13}
Built with