Skip to content

Save and Access Items

Stately’s API offers a small handful of simple methods for creating and accessing your Items.

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
  4. Set up a client to talk to your Store

Saving Items with Put

Using the simple User item type from Define a Schema, we can quickly create new Users and Put them into our Store:

11 collapsed lines
package main
import (
"context"
"time"
"github.com/StatelyCloud/go-sdk/stately"
// This is the code you generated from schema
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
)
func CreateUser(
ctx context.Context,
client stately.Client,
) ([]byte, error) {
user, err := client.Put(ctx, &schema.User{
// No Id is needed, it will be auto-generated by Stately
DisplayName: "Stately Support",
LastLoginDate: time.Now().Unix(),
NumLogins: 1,
})
if err != nil {
return nil, err
}
userID := user.(*schema.User).Id
return userID, nil
}

Our generated code contains typed objects for the User item type, so we can directly create that object and then call client.put to save it. The returned value is the version of the item that was saved in the database, including fields that StatelyDB filled in, such as the User’s new id.

Retrieving Items with Get

We can Get the user back using its key path:

11 collapsed lines
package main
import (
"context"
"time"
"github.com/StatelyCloud/go-sdk/stately"
// This is the code you generated from schema
"github.com/StatelyCloud/stately/go-sdk-sample/schema"
)
func GetUser(
ctx context.Context,
client stately.Client,
userID []byte,
) (*schema.User, error) {
item, err := client.Get(
ctx,
"/usr-"+stately.ToKeyID(userID),
)
if err != nil {
return nil, err
}
return item.(*schema.User), nil
}

More APIs

Put and Get are the two most basic APIs - see Basic Data APIs, List, and Transactions for more things you can do.