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
1
package main
2
3
import (
4
"context"
5
"time"
6
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
func CreateUser(
13
ctx context.Context,
14
client stately.Client,
15
) ([]byte, error) {
16
user, err := client.Put(ctx, &schema.User{
17
// No Id is needed, it will be auto-generated by Stately
18
DisplayName: "Stately Support",
19
20
LastLoginDate: time.Now().Unix(),
21
NumLogins: 1,
22
})
23
if err != nil {
24
return nil, err
25
}
26
userID := user.(*schema.User).Id
27
return userID, nil
28
}

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
1
package main
2
3
import (
4
"context"
5
"time"
6
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
func GetUser(
13
ctx context.Context,
14
client stately.Client,
15
userID []byte,
16
) (*schema.User, error) {
17
item, err := client.Get(
18
ctx,
19
"/usr-"+stately.ToKeyID(userID),
20
)
21
if err != nil {
22
return nil, err
23
}
24
return item.(*schema.User), nil
25
}

More APIs

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