DeliveryToken
DeliveryToken
The DeliveryToken class creates, fetches, lists, updates, and deletes the delivery tokens of a stack. A delivery token gives read-only access to the published content of its environments through the Content Delivery API (CDA).
When to Use This Class: Use this class to issue or revoke the credentials that a website or app uses to read published content.
Conceptual Role: Acts as the Content Management API (CMA) client for the /stacks/delivery_tokens endpoint. Call stack.DeliveryToken() to get an instance.
Returns: DeliveryToken. An object for delivery token operations.
Instance State
The UID you pass to stack.DeliveryToken() decides which methods you can call on the instance:
- Call stack.DeliveryToken() without a UID to work with the token collection, for example Create or Query.
- Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>") to work with one token, for example Fetch or Delete.
If you call a method from the other group, it throws before any request leaves the SDK. A collection method on an instance that has a UID throws InvalidOperationException, and a single-token method on an instance with no UID throws ArgumentException. The Method Index below lists every method in each group.
Warning The SDK treats an empty string as no UID for its checks, but still adds it to the request path. stack.DeliveryToken("") allows Create and Query, which then send their requests to /stacks/delivery_tokens/ with a trailing slash. Call stack.DeliveryToken() with no argument when you mean the collection.
Class-Level Notes
Authentication.
- stack.DeliveryToken() throws InvalidOperationException when the client has no authtoken and the stack has no management token.
- The CMA reference lists only the authtoken for delivery token requests. When the stack has a management token, the SDK sends it instead of the authtoken. Create the stack with client.Stack("<API_KEY>") and no management token.
Requests and responses.
- Every method that sends a request returns the raw API response as a ContentstackResponse. A single token sits under the token key, and the list from Query sits under the tokens key.
- Create and Update serialize a DeliveryTokenModel under the token key and leave out its null properties.
Preview tokens.
- A delivery token can have one preview token for Live Preview. Create it together with the delivery token through the create_with_preview_token query parameter of Create, or later with the PreviewToken class.
Method Index
Work with the token collection. Available on stack.DeliveryToken() when you pass no UID.
| Method Name | Returns | Description |
|---|---|---|
| Query | A new query for delivery tokens. | Builds a query that lists delivery tokens. |
| Create | The API response with the created delivery token. | Creates a delivery token. |
| CreateAsync | The API response with the created delivery token. | Creates a delivery token asynchronously. |
Work with one token. Available on stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
| Method Name | Returns | Description |
|---|---|---|
| Fetch | The API response with the requested delivery token. | Retrieves one delivery token. |
| FetchAsync | The API response with the requested delivery token. | Retrieves one delivery token asynchronously. |
| Update | The API response with the updated delivery token. | Replaces the details of a delivery token. |
| UpdateAsync | The API response with the updated delivery token. | Replaces the details of a delivery token asynchronously. |
| Delete | The API response with the deletion notice. | Deletes a delivery token. |
| DeleteAsync | The API response with the deletion notice. | Deletes a delivery token asynchronously. |
Class-Level Snippet
This snippet creates a delivery token with a preview token, then fetches the new token.
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
Stack stack = client.Stack("<API_KEY>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
ParameterCollection collection = new ParameterCollection();
collection.Add("create_with_preview_token", true);
try
{
ContentstackResponse created = await stack.DeliveryToken().CreateAsync(model, collection);
string? tokenUid = created.OpenJsonObjectResponse()["token"]?["uid"]?.GetValue<string>();
ContentstackResponse token = await stack.DeliveryToken(tokenUid).FetchAsync();
Console.WriteLine(token.OpenJsonObjectResponse()["token"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}Methods
Each method of the DeliveryToken class has its own section below, with its parameters, validation rules, behavior, and examples.
Create
Create creates a delivery token with the name, description, and scope you set in the model.
| Name | Type | Description |
|---|---|---|
| model (required) | DeliveryTokenModel | Sets the token name, description, and scope. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the created delivery token.
Validation
- Throws InvalidOperationException if the instance has a UID, because Create posts to the delivery token collection. Call stack.DeliveryToken() with no UID.
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- The SDK does not check the properties of model. The API validates them.
- Throws ContentstackErrorException when the API rejects the request, for example an environment that does not exist in the stack.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one POST request to /stacks/delivery_tokens, with model serialized under a token key. See Create delivery token in the Content Management API (CMA) reference.
- Leaves every null property of model out of the request body.
- The CMA reference accepts the create_with_preview_token query parameter. Set it to true in collection to create a preview token together with the delivery token.
- The CMA reference lists only the authtoken for this request. When the stack has a management token, the SDK sends it instead of the authtoken, so create the stack without one.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response. The created token sits under the token key.
Example
Basic usage: create a delivery token for one environment and branch
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Description = "Read-only access to published content in production",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
},
new DeliveryTokenScope()
{
Module = "branch",
Branches = new List<string> { "main" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken().Create(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: create a delivery token with a preview token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
ParameterCollection collection = new ParameterCollection();
collection.Add("create_with_preview_token", true);
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken().Create(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Edge case: calling Create on an instance that has a UID
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
try
{
// Create adds a new delivery token, so call it on DeliveryToken() with no UID.
client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Create(model);
}
catch (InvalidOperationException e)
{
// Fires before any request, because the instance has a UID
Console.WriteLine(e.Message);
}CreateAsync
CreateAsync creates a delivery token with the name, description, and scope you set in the model, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| model (required) | DeliveryTokenModel | Sets the token name, description, and scope. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the created delivery token.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws InvalidOperationException if the instance has a UID, because CreateAsync posts to the delivery token collection. Call stack.DeliveryToken() with no UID.
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- The SDK does not check the properties of model. The API validates them.
- The SDK throws the client-side exceptions above when you call CreateAsync, before it returns a task. An API rejection surfaces when you await the task.
- Throws ContentstackErrorException when the API rejects the request, for example an environment that does not exist in the stack.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Completes the task when the API responds.
- Otherwise behaves like Create.
Example
Basic usage: create a delivery token for one environment and branch
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Description = "Read-only access to published content in production",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
},
new DeliveryTokenScope()
{
Module = "branch",
Branches = new List<string> { "main" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken().CreateAsync(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: create a delivery token with a preview token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
ParameterCollection collection = new ParameterCollection();
collection.Add("create_with_preview_token", true);
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken().CreateAsync(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Delete
Delete deletes one delivery token from the stack.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters, for example force. |
Returns: ContentstackResponse. The API response with the deletion notice.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentException (parameter uid) if the instance has no UID, because Delete removes one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one DELETE request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>. See Delete delivery token in the Content Management API (CMA) reference.
- The CMA reference accepts the optional force query parameter. Set it to true in collection to force the deletion.
- The CMA reference lists only the authtoken for this request. When the stack has a management token, the SDK sends it instead of the authtoken, so create the stack without one.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response.
Warning Deleting a delivery token stops every app that uses it from reading content through the Content Delivery API. The SDK offers no way to restore the token.
Example
Basic usage: delete a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Delete();
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: force the deletion of a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ParameterCollection collection = new ParameterCollection();
collection.Add("force", true);
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Delete(collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}DeleteAsync
DeleteAsync deletes one delivery token from the stack, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters, for example force. |
Returns: Task<ContentstackResponse>. The API response with the deletion notice.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentException (parameter uid) if the instance has no UID, because DeleteAsync removes one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- The SDK throws the client-side exceptions above when you call DeleteAsync, before it returns a task. An API rejection surfaces when you await the task.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Completes the task when the API responds.
- Otherwise behaves like Delete.
Warning Deleting a delivery token stops every app that uses it from reading content through the Content Delivery API. The SDK offers no way to restore the token.
Example
Basic usage: delete a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").DeleteAsync();
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: force the deletion of a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ParameterCollection collection = new ParameterCollection();
collection.Add("force", true);
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").DeleteAsync(collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Fetch
Fetch retrieves the details of one delivery token.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the requested delivery token.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentException (parameter uid) if the instance has no UID, because Fetch reads one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one GET request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>. See Get a single delivery token in the Content Management API (CMA) reference.
- The CMA reference lists only the authtoken for this request. When the stack has a management token, the SDK sends it instead of the authtoken, so create the stack without one.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response. The token sits under the token key.
Example
Basic usage: fetch a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Fetch();
Console.WriteLine(response.OpenJsonObjectResponse()["token"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: fetch a delivery token with a query parameter collection
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Fetch(collection);
Console.WriteLine(response.OpenJsonObjectResponse()["token"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}FetchAsync
FetchAsync retrieves the details of one delivery token, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the requested delivery token.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentException (parameter uid) if the instance has no UID, because FetchAsync reads one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- The SDK throws the client-side exceptions above when you call FetchAsync, before it returns a task. An API rejection surfaces when you await the task.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Completes the task when the API responds.
- Otherwise behaves like Fetch.
Example
Basic usage: fetch a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").FetchAsync();
Console.WriteLine(response.OpenJsonObjectResponse()["token"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: fetch a delivery token with a query parameter collection
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").FetchAsync(collection);
Console.WriteLine(response.OpenJsonObjectResponse()["token"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Query
Query returns a query object that lists the delivery tokens of the stack when you call Find or FindAsync on it.
This method takes no parameters. Chain Limit, Skip, and IncludeCount on the query object, then call Find or FindAsync to send the request.
Returns: Query. A new query for delivery tokens.
Validation
- Throws InvalidOperationException if the instance has a UID, because Query lists the delivery token collection. Call stack.DeliveryToken() with no UID.
- Query sends no request. Errors that do occur come from the Find or FindAsync call that follows, not from Query.
- Find and FindAsync throw InvalidOperationException if the client has no authtoken and the stack has no management token, and ContentstackErrorException when the API rejects the request.
- Limit, Skip, and IncludeCount each add a query parameter to a dictionary that rejects duplicate keys. The SDK throws ArgumentException before any request in the following two cases:
- You chain the same method twice.
- The collection you pass to Find repeats a key that a chained method already set.
- A query object keeps its parameters after Find runs. A second Find call with a collection that repeats a key throws ArgumentException. Build a new query for each request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Find and FindAsync send one GET request to /stacks/delivery_tokens, with every parameter you set in the query string. See Get all delivery tokens in the Content Management API (CMA) reference.
- Returns the raw API response. The tokens sit under the tokens key.
- Does not page through the results. Without Limit and Skip, the API applies its own page size. To read the next page, build a new query with a higher Skip value.
- The query object has the five methods below. See the Query class for details.
| Method | Returns | Description |
|---|---|---|
| Limit(double value) | Query | Sets the number of delivery tokens per page. |
| Skip(double value) | Query | Sets the number of delivery tokens to skip. |
| IncludeCount() | Query | Adds the total count to the response. |
| Find(ParameterCollection? collection = null) | ContentstackResponse | Sends the request. |
| FindAsync(ParameterCollection? collection = null) | Task<ContentstackResponse> | Sends the request without blocking the calling thread. |
Example
Basic usage: list the delivery tokens of a stack
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken().Query().Find();
Console.WriteLine(response.OpenJsonObjectResponse()["tokens"]?.ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}Paging: read one page with a total count
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken()
.Query()
.Limit(10)
.Skip(0)
.IncludeCount()
.FindAsync();
Console.WriteLine($"Total: {response.OpenJsonObjectResponse()["count"]}");
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}Edge case: setting a query parameter twice
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
Query query = client.Stack("<API_KEY>").DeliveryToken().Query().Limit(5);
ParameterCollection collection = new ParameterCollection();
collection.Add("limit", 10);
try
{
// Limit(5) already set "limit", so adding it again fails.
query.Find(collection);
}
catch (ArgumentException e)
{
// Fires before any request, when a query parameter is set twice
Console.WriteLine(e.Message);
}Update
Update replaces the name, description, and scope of a delivery token with the values in the model.
| Name | Type | Description |
|---|---|---|
| model (required) | DeliveryTokenModel | Sets the full token, including its scope. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: ContentstackResponse. The API response with the updated delivery token.
Validation
- Throws ArgumentException (parameter uid) if the instance has no UID, because Update changes one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Sends one PUT request to /stacks/delivery_tokens/<DELIVERY_TOKEN_UID>, with model serialized under a token key. See Update delivery token in the Content Management API (CMA) reference.
- Leaves every null property of model out of the request body.
- The CMA reference lists only the authtoken for this request. When the stack has a management token, the SDK sends it instead of the authtoken, so create the stack without one.
- Adds the entries of collection to the query string. null or an empty collection adds none.
- Returns the raw API response. The updated token sits under the token key.
Example
Basic usage: change the scope of a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Description = "Read-only access to published content in production",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
},
new DeliveryTokenScope()
{
Module = "branch",
Branches = new List<string> { "main" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Update(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: update a delivery token with a query parameter collection
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").Update(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}UpdateAsync
UpdateAsync replaces the name, description, and scope of a delivery token with the values in the model, without blocking the calling thread.
| Name | Type | Description |
|---|---|---|
| model (required) | DeliveryTokenModel | Sets the full token, including its scope. |
| collection | ParameterCollection? | Adds query parameters to the request. |
Returns: Task<ContentstackResponse>. The API response with the updated delivery token.
Validation
- Throws InvalidOperationException if the client has no authtoken and the stack has no management token, because the request needs credentials.
- Throws ArgumentException (parameter uid) if the instance has no UID, because UpdateAsync changes one delivery token. Call stack.DeliveryToken("<DELIVERY_TOKEN_UID>").
- Throws ArgumentNullException (parameter dataModel) if model is null, because the SDK builds the request body from it.
- The SDK throws the client-side exceptions above when you call UpdateAsync, before it returns a task. An API rejection surfaces when you await the task.
- Throws ContentstackErrorException when the API rejects the request.
Additional Resource Refer to Content Management API Errors for the full error code list.
Behavior
- Completes the task when the API responds.
- Otherwise behaves like Update.
Example
Basic usage: change the scope of a delivery token
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Description = "Read-only access to published content in production",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
},
new DeliveryTokenScope()
{
Module = "branch",
Branches = new List<string> { "main" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").UpdateAsync(model);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}All parameters: update a delivery token with a query parameter collection
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
DeliveryTokenModel model = new DeliveryTokenModel()
{
Name = "Production Website",
Scope = new List<DeliveryTokenScope>
{
new DeliveryTokenScope()
{
Module = "environment",
Environments = new List<string> { "<ENVIRONMENT_UID>" },
ACL = new Dictionary<string, string> { ["read"] = "true" }
}
}
};
// The CMA reference lists no query parameters for this request. Add any parameter the API accepts.
ParameterCollection collection = new ParameterCollection();
collection.Add("<PARAMETER_NAME>", "<PARAMETER_VALUE>");
try
{
ContentstackResponse response = await client.Stack("<API_KEY>").DeliveryToken("<DELIVERY_TOKEN_UID>").UpdateAsync(model, collection);
Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
Console.WriteLine(e.ErrorMessage);
}