Create a Client
Let’s get your backend code talking to StatelyDB so you can start reading and writing data.
Prerequisites
- Create a Stately account and a Store
- Install an SDK for your language
- 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}
# This is the code you generated from schemarequire_relative "./schema/stately"
client = StatelyDB::Client.new(store_id: 12345, region: "us-west-2")
# This is the code you generated from schemafrom .schema import Client
client = Client(store_id=12345, region="us-west-2")
// This is the code you generated from schemaimport { createClient } from "./schema/index.js";
// Create a Data client for interacting with a Storeconst client = createClient( 12345n, // Store ID { region: "us-west-2" },);
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}
# This is the code you generated from schemarequire_relative "./schema/stately"
client = StatelyDB::Client.new( store_id: 12345, region: "us-west-2", token_provider: StatelyDB::Common::Auth::AuthTokenProvider.new( access_key: "my-access-key" ))
# The StatelyDB SDKfrom statelydb import init_server_auth
# This is the code you generated from schemafrom .schema import Client
client = Client( store_id=12345, region="us-west-2", token_provider=init_server_auth( access_key="my-access-key", ),)
// The StatelyDB SDKimport { accessKeyAuth } from "@stately-cloud/client";// This is the code you generated from schemaimport { createClient } from "./schema/index.js";
// Create a Data client for interacting with a Storeconst client = createClient( 12345n, // Store ID { region: "us-west-2", authTokenProvider: accessKeyAuth({ accessKey: "my-access-key", }), },);