# Godot

**URL:** https://heroiclabs.com/docs/satori/client-libraries/godot/
**Summary:** The official Godot client handles all communication with the Satori server, enabling management of your live game via sending analytics events, updating properties, getting feature flags and experiments, and more.
**Keywords:** Godot client guide, Godot client, satori Godot, authentication, user accounts, audiences, experiments, live events, feature flags, identities, liveops Godot
**Categories:** satori, godot, client-libraries

---


# Satori Godot Client Guide

This client library guide will show you how to use the core Satori features in **Godot**.

## Prerequisites

Before proceeding ensure that you have:

* Access to Satori server instance
* Installed the [Satori Godot SDK](#installation)
* Using Godot 4 engine.

{{< note "important" >}}
The Satori client is packaged as part of the Nakama Godot SDK, but using Nakama **is not** required.
{{< / note >}}
<!--
### Full API documentation

 update link to point to Satori 

For the full API documentation please visit the [API docs](https://heroiclabs.github.io/nakama-godot/).-->

### Installation

The client is available on:

* [Godot Asset Library](https://godotengine.org/asset-library/asset)
* [Heroic Labs GitHub Releases](https://github.com/heroiclabs/nakama-godot/releases/latest)

Add the `Satori.gd` singleton (in `addons/com.heroiclabs.nakama/`) as an [autoload in Godot](https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html).

### Updates

New versions of the Godot Client and the corresponding improvements are documented in the [Release Notes](/docs/nakama/getting-started/release-notes/).

## Getting started

Learn how to get started using the Satori Client to manage your live game.

### Satori client

The Satori Client connects to a Satori Server and is the entry point to access Satori features. It is recommended to have one client per server per game.

To create a client pass in your server connection details:

```gdscript
extends Node

func ready():
	var scheme = "http"
	var host = "127.0.0.1"
	var port: Int = 7450
	var apiKey = "apiKey"
	var client := Satori.create_client(apiKey, host, port, scheme)
```

### Configuring the request timeout length

Each request to Satori from the client must complete in a certain period of time before it is considered to have timed out. You can configure how long this period is (in seconds) by setting the `timeout` value when creating the client:

```gdscript
extends Node

func ready():
	var scheme = "http"
	var host = "127.0.0.1"
	var port: Int = 7450
	var apiKey = "apiKey"
	var timeout = 10
	var client := Satori.create_client(apiKey, host, port, scheme, timeout)
```

{{< note "important" "API Keys">}}
Create and rotate API keys in the [Satori Settings page](../../concepts/settings/#api-keys).
{{< / note >}}

## Authentication

Authenticate users using the Satori Client via their unique ID.

When authenticating, you can optionally pass in any desired `default_properties` and/or `custom_properties` to be updated. If none are provided, the properties remain as they are on the server.

```gdscript
var id = "<UniqueIdentity>"

var default_properties = {
    "platform": OS.get_name(),
    "language": OS.get_locale(),
    "client_version": ProjectSettings.get_setting("application/config/version"),
    "version": ProjectSettings.get_setting("application/config/version")
}

var session = await client.authenticate_async(id, default_properties)
if session.is_exception():
    print("An error occurred: %s" % session)
    return
print("Successfully authenticated: %s" % session)
```

When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `Session` object.


### Session lifecycle

Sessions expire after five (5) minutes by default. Expiring inactive sessions is good security practice.

Satori provides ways to restore sessions, for example when players re-launch the game, or refresh tokens to keep the session active while the game is being played.

Use the auth and refresh tokens on the session object to restore or refresh sessions.

Restore a session without having to re-authenticate:

```gdscript
var auth_token = "restored from save location"
var session = SatoriClient.restore_session(auth_token)
```
#### Automatic session refresh

The Godot client library includes a feature where sessions close to expiration are automatically refreshed.

This is enabled by default but can be configured when first creating the [Satori client](#satori-client) using the following parameters:

* `auto_refresh` - Boolean value indicating if this feature is enabled, `true` by default

Check if a session has expired or is close to expiring and refresh it to keep it alive:

#### Manually refreshing a session

Sessions can be manually refreshed.

```gdscript
session = await client.session_refresh_async(session)
```

### Ending sessions

Logout and end the current session:

```gdscript
await client.authenticate_logout_async(session)
```

## Experiments

Satori [Experiments](../../concepts/experiments/) allow you to test different game features and configurations in a live game.

List the current experiments for this user:

```gdscript
await client.get_all_experiments_async(session)
```

You can also specify an array of experiment names you wish to get:

```gdscript
var experiments = await client.get_experiments_async(session, ["ExperimentOne", "ExperimentTwo"])
```

## Feature flags

Satori [feature flags](../../concepts/remote-configuration/) allow you to enable or disable features in a live game.

### Get a single flag

Get a single feature flag for this user:

```gdscript
var flag = await client.get_flag_async(session, "FlagName")
```

You can also specify a default value for the flag if a value cannot be found:

```gdscript
var flag = await client.get_flag_async(session, "FlagName", "DefaultValue")
```

Specifying a default value ensures no exception will be thrown if the network is unavailable, instead a flag with the specified default value will be returned.

### Get a single default flag

Get a single default flag for this user:

```gdscript
var flag = await client.get_flag_default_async(session, "FlagName")
```

Similar to the `get_flag_async` method, you can also provide a default value for default flags:

```gdscript
var flag = await client.get_flag_default_async(session, "FlagName", "DefaultValue")
```

Specifying a default value ensures no exception will be thrown if the network is unavailable, instead a flag with the specified default value will be returned.

### Listing identity flags

List the available feature flags for this user:

```gdscript
var flags = await client.get_flags_async(session)
```

### Listing default flags

List all default feature flags:

```gdscript
var flags = await client.get_flags_default_async()
```
## Events

Satori [Events](../../concepts/performance-monitoring/understand-events/) allow you to send data for a given user to the server for processing.

{{< note "important" "Metadata Limits">}}
The maximum size of the `metadata` field is `4096` bytes.
{{< / note >}}

### Sending single events

```gdscript
var _now = Time.get_unix_time_from_system()
await client.event_async(session, Event.new("gameFinished", _now))
```

### Sending multiple events

```gdscript
var _now = Time.get_unix_time_from_system()
await client.events_async(session, [Event.new("adStarted", _now), Event.new("appLaunched", _now)])
```

## Live events

Satori [Live Events](../../concepts/live-events/) allow you to deliver established features to your players on a custom schedule.

### List all available live events:

```gdscript
var live_events = await client.get_live_events_async(session)
```

### Join explicit live events

```gdscript
await client.join_live_events_async(session, liveEventID);
```

## Identities

Satori [Identities](/docs/satori/concepts/segmentation/) identify individual players of your game and can be enriched with custom properties.

### List an identity's properties

```gdscript
var properties = await client.list_properties_async(session)
```

### Update an identity's properties

```gdscript
var default_properties = {
	"DefaultPropertyKey": "DefaultPropertyValue",
  	"AnotherDefaultPropertyKey": "AnotherDefaultPropertyValue"
}

var custom_properties = {
	"CustomPropertyKey": "CustomPropertyValue",
	"AnotherCustomPropertyKey": "AnotherCustomPropertyValue"
}

await client.update_properties_async(session, default_properties, custom_properties)
```

You can immediately re-evaluate the user's audience memberships upon updating their properties by passing `recompute` as `true`:

```gdscript
var recompute = true

await client.update_properties_async(session, default_properties, custom_properties, recompute)
```

### Identifying a session with a new ID

If you want to submit events to Satori before a user has authenticated with the game backend (e.g. Nakama) and has a User ID, you should authenticate with Satori using a temporary ID, such as the device's unique identifier or a randomly generated one.

```gdscript
var device_id = OS.get_unique_id()

# Authenticate with Satori
var session = client.authenticate(device_id)
```

You can then submit events before the user has authenticated with the game backend.

```gdscript
var _now = Time.get_unix_time_from_system()
await client.event_async(session, Event.new("gameFinished", _now))
```

The user would then authenticate with the game backend and retrieve their User ID from the session:

```gdscript
var nakama_session = await nakamaClient.authenticate_custom_async("<nakamaCustomId>")
var nakama_uid = nakama_session.id
```

Once a user has successfully authenticated, you should then call `identify_sync` to enrich the current session and return a new session that should be used for submitting future events.

```gdscript
var new_session = await client.identify_async(session, nakama_uid, default_properties, custom_properties)
```

Note that the old session is no longer valid and cannot be used after this.

### Deleting an identity

Delete the calling identity and its associated data:

```gdscript
await client.delete_identity_async(session)
```
