Skip to main content

Storing initial task state

This section provides information on how to store initial state for a task in the client application. This state can be linked to task arguments and used in a command.

API reference:

Required scopes:

  • Data/Buildby.Taskurai/state/create

When using transactions, these additional scopes are required:

  • Data/Buildby.Taskurai/state/delete

Optional scopes:

  • Data/Buildby.Taskurai/sensitive/read: Can return sensitive data.

Prerequisites

You have completed:

Saving state for a task

In this example, an initial large json file is uploaded to the default state store and a task is created that accepts a state reference as argument.

// Get state client
var stateClient = taskurai.GetStateClient();

// Create a task to upload daily sales
var createTaskInput = new TaskConfig("CreateCart")
{
Arguments =
[
new TaskArgument("SessionId")
{
Value = sessionId
}
],
Category = "Webshop",
Subject = "Create a cart for a unregistered customer"
};

// Upload large daily sales json to state storage
var state = stateClient.SaveBlobState(
"DefaultStateStore",
new StateInput(createTaskInput, "dailysales") // Link state to task
{
HasBlob = true,
Ttl = 30 * 24 * 60 * 60, // one month
ContentType = "application/json"
},
"dailysales.json",
overwrite: true);

createTaskInput.Arguments.Add(new TaskArgument("dailySales")
{
StateReference = state.StateReference
});

var createdTask = taskurai.CreateTask(createTaskInput);

Console.WriteLine(JsonSerializer.Serialize(createdTask, serializerOptions));

Next, run the console application to validate:

dotnet run

Sample output:

{
"id": "b58fa450-5c71-4f07-a84c-058bbcd1e45e",
"config": {
"command": "CreateCart",
"arguments": [
{
"name": "SessionId",
"data": "f91d0f4f-026a-411b-8e84-1f52eded5b99"
},
{
"name": "dailySales",
"stateReference": {
"stateStoreName": "DefaultStateStore",
"id": "isolation::dd10b001-addd-45c2-94a6-932282b7515c::task::b58fa450-5c71-4f07-a84c-058bbcd1e45e::state::dailysales"
}
}
],
"secrets": [],
"category": "Webshop",
"subject": "Create a cart for a unregistered customer",
"tags": {},
"isolationMode": true,
"isolationKey": "dd10b001-addd-45c2-94a6-932282b7515c",
"correlationId": "b58fa450-5c71-4f07-a84c-058bbcd1e45e",
"traceParent": "00-b58fa4505c714f07a84c058bbcd1e45e-abc36f03ff7b1132-01",
"id": "b58fa450-5c71-4f07-a84c-058bbcd1e45e"
},
"created": "2026-04-01T10:19:29.1131616+00:00",
"createdBy": "d8e9cb3f-ffeb-4915-bb15-6681a3bcd175",
"modified": "2026-04-01T10:19:29.3469342+00:00",
"modifiedBy": "d8e9cb3f-ffeb-4915-bb15-6681a3bcd175",
"status": "created",
"archived": false,
"postponed": false,
"deleting": false,
"correlationId": "b58fa450-5c71-4f07-a84c-058bbcd1e45e",
"defaultStateStore": "DefaultStateStore"
}

Saving blob state for a task

In this example, an initial large json file is uploaded to the default state store and a task is created that accepts a state reference as argument.

// Get state client
var stateClient = taskurai.GetStateClient();

// Create a task to upload daily sales
var createTaskInput = new TaskConfig("UploadDailySalesSAP")
{
Arguments =
[
new TaskArgument("date")
{
Value = salesDate
}
],
Category = "Retail sales",
Subject = "Upload daily sales and send aggregates to SAP."
};

// Upload large daily sales json to state storage
var state = stateClient.SaveBlobState(
"DefaultStateStore",
new StateInput(createTaskInput, "dailysales") // Link state to task
{
HasBlob = true,
Ttl = 30 * 24 * 60 * 60, // one month
ContentType = "application/json"
},
"dailysales.json",
overwrite: true);

createTaskInput.Arguments.Add(new TaskArgument("dailySales")
{
StateReference = state.StateReference
});

var createdTask = taskurai.CreateTask(createTaskInput);

Console.WriteLine(JsonSerializer.Serialize(createdTask, serializerOptions));

Next, run the console application to validate:

dotnet run

Sample output:

{
"id": "395784b5-e123-4a1c-847a-517a685a288f",
"config": {
"command": "UploadDailySalesSAP",
"arguments": [
{
"name": "date",
"data": "2026-04-01T10:26:46.414571\u002B00:00"
},
{
"name": "dailySales",
"stateReference": {
"stateStoreName": "DefaultStateStore",
"id": "isolation::dd10b001-addd-45c2-94a6-932282b7515c::task::395784b5-e123-4a1c-847a-517a685a288f::state::dailysales"
}
}
],
"secrets": [],
"category": "Retail sales",
"subject": "Upload daily sales and send aggregates to SAP.",
"tags": {},
"isolationMode": true,
"isolationKey": "dd10b001-addd-45c2-94a6-932282b7515c",
"correlationId": "395784b5-e123-4a1c-847a-517a685a288f",
"traceParent": "00-395784b5e1234a1c847a517a685a288f-6161454afd1f2991-01",
"id": "395784b5-e123-4a1c-847a-517a685a288f"
},
"created": "2026-04-01T10:26:48.2528242+00:00",
"createdBy": "d8e9cb3f-ffeb-4915-bb15-6681a3bcd175",
"modified": "2026-04-01T10:26:48.370727+00:00",
"modifiedBy": "d8e9cb3f-ffeb-4915-bb15-6681a3bcd175",
"status": "created",
"archived": false,
"postponed": false,
"deleting": false,
"correlationId": "395784b5-e123-4a1c-847a-517a685a288f",
"defaultStateStore": "DefaultStateStore"
}