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 an Access Key. You can create access keys in the console by clicking “Manage Access Keys”. The access key is a secret that can be passed explicitly when creating a client, or it will be picked up automatically from a STATELY_ACCESS_KEY environment variable.

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

11 collapsed lines
package main
import (
"context"
// The StatelyDB SDK
"github.com/StatelyCloud/go-sdk/stately"
// This is the code you generated from schema
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
)
// Create a client for interacting with a Store.
func makeClient() stately.Client {
// This uses the generated "NewClient" func in your schema package.
dataClient, err := schema.NewClient(
context.TODO(),
12345, // Store ID
&stately.Options{Region: "us-west-2"},
)
if err != nil {
panic("failed to build StatelyDB client: " + err.Error())
}
return dataClient
}

Providing an explicit Access Key

If you don’t want your access key to be read from environment variables, you can provide them explicitly:

11 collapsed lines
package main
import (
"context"
// The StatelyDB SDK
"github.com/StatelyCloud/go-sdk/stately"
// This is the code you generated from schema
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
)
// Create a client for interacting with a Store.
func makeClientWithCredentials() stately.Client {
// Use the generated "NewClient" func in your schema package.
dataClient, err := schema.NewClient(
context.TODO(),
12345, // Store ID
&stately.Options{
Region: "us-west-2",
AccessKey: "my-access-key",
},
)
if err != nil {
panic("failed to build StatelyDB client: " + err.Error())
}
return dataClient
}