State Provider

The State provider allows you to store data on the Vonage Cloud Runtime platform for your project instances to use. The state provider has a key-value store at its root. As well as lists and maps with various operations you can perform on them.

Functions

You can use the following operations for storing data using the State provider:

Initializing the State Provider

To can access the state provider from a session:

This state is scoped to the session passed into the state initializer, so it can be referred to as session state. When the session reaches its TTL, the stored state will be deleted.

On subsequent calls to your code, you will want to make sure that you are using the same session to have access to your state. You can do so by initializing your instance from an incoming request:

Account State

Account state is scoped to your Vonage account and is available between all your projects and their instances.

Instance State

Instance state is scoped to your instance and deleted when the instance is destroyed. This lets you share context between different sessions in your application.

Use Case

For example, you can store, retrieve, and delete objects on a session using the key-value operations:

Or you can use instance state, and the hash table operations to persist data between sessions:

This is storing the customer information in a table with the customer's phone number as the key.

Using the customer's number you can retrieve the customer information that was stored earlier.

curl --location --request POST '{INSTANCE_ENDPOINT}/add-customer' \--header 'Content-Type: application/json' \--data-raw '{ "id":"1", "name": "John", "phone": "101"}'curl --location --request GET '{INSTANCE_ENDPOINT}/on-phone-call?number=101'