Skip to content

Create a Client

Let’s get your backend code talking to StatelyDB so you can start reading and writing data.

Prerequisites

  1. Create a Stately account and a Store
  2. Install an SDK for your language
  3. Define a schema and generate language-specific code

Creating a Client

When you generated code for your schema, you got a customized client creation function that knows about all of your schema’s types. You can import that function to create a client, and then use that client from your backend code to interact with StatelyDB.

To authenticate your client to the StatelyDB service, you will need both a Client ID and Client Secret. During this preview period you’ll have to get those by contacting [email protected]. These can be passed explicitly when creating a client, or they will be picked up automatically from STATELY_CLIENT_ID and STATELY_CLIENT_SECRET environment variables.

You will also need to specify your Store ID and the region for your store. Both of those are available in the web console, or by running stately whoami with the CLI.

11 collapsed lines
1
package main
2
3
import (
4
"context"
5
6
// The StatelyDB SDK
7
"github.com/StatelyCloud/go-sdk/stately"
8
// This is the code you generated from schema
9
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
10
)
11
12
// Create a client for interacting with a Store.
13
func makeClient() stately.Client {
14
// This uses the generated "NewClient" func in your schema package.
15
dataClient, err := schema.NewClient(
16
context.TODO(),
17
12345, // Store ID
18
&stately.Options{Region: "us-west-2"},
19
)
20
if err != nil {
21
panic("failed to build StatelyDB client: " + err.Error())
22
}
23
return dataClient
24
}

Providing an explicit Client ID and Client Secret

If you don’t want Client ID and Client Secret to be read from environment variables, you can provide them explicitly:

11 collapsed lines
1
package main
2
3
import (
4
"context"
5
6
// The StatelyDB SDK
7
"github.com/StatelyCloud/go-sdk/stately"
8
// This is the code you generated from schema
9
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
10
)
11
12
// Create a client for interacting with a Store.
13
func makeClientWithCredentials() stately.Client {
14
// Use the generated "NewClient" func in your schema package.
15
dataClient, err := schema.NewClient(
16
context.TODO(),
17
12345, // Store ID
18
&stately.Options{
19
Region: "us-west-2",
20
ClientID: "my-client-id",
21
ClientSecret: "my-client-secret",
22
},
23
)
24
if err != nil {
25
panic("failed to build StatelyDB client: " + err.Error())
26
}
27
return dataClient
28
}