PreviewToken
PreviewToken
The PreviewToken class creates and deletes preview tokens scoped to a specific delivery token.
When to Use This Class: Use this class when you need to programmatically create or revoke preview tokens for a specific delivery token without navigating the Contentstack UI.
Conceptual Role: Acts as a scoped token manager for a single delivery token. Call client.Stack(apiKey).PreviewToken(deliveryTokenUid) to target it and enable Create, CreateAsync, Delete, and DeleteAsync.
Warning: deliveryTokenUid is a positional string parameter. The SDK does not validate it client-side. Passing an incorrect or empty value will cause a runtime API error (typically 422 or 404) with no indication that the constructor argument is the source. Always verify the delivery token UID before calling any method on this class.
Class-Level Notes
Endpoint Compatibility: All methods in this class target the rest-preview.contentstack.com endpoint exclusively. They are not compatible with the standard cdn.contentstack.io delivery endpoint. Ensure your ContentstackClient is initialized with the preview endpoint when using this class.
Authentication: All methods require a valid auth token passed to ContentstackClient. For 401 (missing or invalid auth token) and 403 (insufficient permissions) errors, see Management API Errors.
Rate Limiting & Retry: This SDK automatically retries requests that return 429 (Too Many Requests) and 5xx errors. By default, RetryLimit is 5 attempts and RetryDelay is 300 ms, with the delay increasing between each retry attempt. See Retry Mechanism to configure retry limits and delay.
Query Parameters: All methods accept an optional collection parameter of type ParameterCollection, used to append query parameters to the request. The default value is null.
Async Methods: Always use await at the call site. If an async method is not awaited directly, exceptions are thrown as AggregateException instead of surfacing directly.
Method Index
| Method Name | Returns | Description |
|---|---|---|
| Create | ContentstackResponse | Creates a preview token. |
| CreateAsync | Task<ContentstackResponse> | Creates a preview token asynchronously. |
| Delete | ContentstackResponse | Deletes a preview token. |
| DeleteAsync | Task<ContentstackResponse> | Deletes a preview token asynchronously. |
Create a Preview Token
Creates a preview token for a delivery token and handles errors. Demonstrates the workflow from client initialization to API response.
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
// Initialize the client with your auth token
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// Build the model describing the preview token
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
try
{
// Create the preview token scoped to a specific delivery token
ContentstackResponse response = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Create(model);
}
catch (Exception ex)
{
// A 404 or 422 error usually indicates an invalid or missing deliveryTokenUid.
Console.WriteLine($"Error creating preview token: {ex.Message}");
}Create
Creates a preview token for the specified delivery token in the stack.
Validation
Throws InvalidOperationException with message "Operation not allowed on this model. Update your request and try again." if the PreviewToken object already has a UID assigned. Create() only supports creating new preview tokens.
For authentication and rate-limit errors (401, 403, 429), see Class-Level Notes.
| HTTP Status | Cause | Fix |
|---|---|---|
| 404 | The deliveryTokenUid passed to the constructor does not exist in this stack. | Verify the delivery token UID in the Contentstack UI before calling. |
| 422 | The request payload is invalid, malformed, or missing required fields. | Ensure PreviewTokenModel has all required fields set before calling Create(). |
Behavior
Sends a single HTTP POST request to the Management API and returns the created preview token in the response.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | The request payload containing preview token details. |
| collection | ParameterCollection | Optional query parameters appended to the request. See Class-Level Notes. |
Basic usage — create a preview token for a delivery token
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
// Initialize the client
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// Build the preview token model
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
try
{
// Create the preview token for the specified delivery token
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Create(model);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}All parameters — with a ParameterCollection
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);
try
{
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Create(model, collection);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}Error handling — catching a failed token creation
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
try
{
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Create(model);
}
catch (ContentstackErrorException ex)
{
Console.WriteLine($"API error {ex.StatusCode}: {ex.ErrorMessage}");
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"State error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}CreateAsync
Asynchronously creates a preview token for the specified delivery token in the stack.
Validation
Throws InvalidOperationException with message:
- "You are not logged in. Log in and try again." if the client has no active auth session.
- "Operation not allowed on this model. Update your request and try again." if the
PreviewTokenobject already has a UID assigned.CreateAsync()only supports creating new preview tokens.
For authentication and rate-limit errors (401, 403, 429), see Class-Level Notes.
| HTTP Status | Cause | Fix |
|---|---|---|
| 404 | The deliveryTokenUid passed to the constructor does not exist in this stack. | Verify the delivery token UID in the Contentstack UI before calling. |
| 422 | The request payload is invalid, malformed, or missing required fields. | Ensure PreviewTokenModel has all required fields set before calling CreateAsync(). |
Behavior
Executes the HTTP POST asynchronously and does not block the calling thread. Retry behavior applies. See Class-Level Notes, Rate Limiting & Retry.
| Name | Type | Description |
|---|---|---|
| model (required) | PreviewTokenModel | The request payload containing preview token details. |
| collection | ParameterCollection | Optional query parameters appended to the request. See Class-Level Notes. |
Basic usage — asynchronously create a preview token
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
// Initialize the client
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
// Build the preview token model
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
try
{
// Await the async create call
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.CreateAsync(model);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}All parameters — async with a ParameterCollection
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);
try
{
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.CreateAsync(model);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}Error handling — catching async exceptions
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
PreviewTokenModel model = new PreviewTokenModel()
{
Name = "Sample Preview Token",
Description = "This is a sample preview token."
};
try
{
// Always await directly to surface exceptions correctly
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.CreateAsync(model);
}
catch (ContentstackErrorException ex)
{
Console.WriteLine($"API error {ex.StatusCode}: {ex.ErrorMessage}");
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"State error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}Delete
Deletes the preview token associated with the specified delivery token.
This method does not require a separate preview token UID. It deletes the preview token associated with the deliveryTokenUid passed to the class constructor.
Warning: This operation is irreversible. Deleted preview tokens cannot be recovered. Verify the delivery token UID before calling this method.
Validation
Throws InvalidOperationException with message "You are not logged in. Log in and try again." if the client has no active auth session.
For authentication and rate-limit errors (401, 403, 429), see Class-Level Notes.
| HTTP Status | Cause | Fix |
|---|---|---|
| 404 | The deliveryTokenUid does not exist or has no associated preview token. | Verify the delivery token UID before calling Delete(). |
Behavior
Sends a single HTTP DELETE request to the Management API. This operation is irreversible and cannot be undone via the SDK.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection | Optional query parameters appended to the request. See Class-Level Notes. |
Basic usage — delete the preview token for a delivery token
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
// Initialize the client
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
// Delete the preview token scoped to the specified delivery token
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Delete();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}All parameters — with a ParameterCollection
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);
try
{
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Delete(collection);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}Error handling — catching a 404 when the preview token does not exist
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
ContentstackResponse contentstackResponse = client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.Delete();
}
catch (ContentstackErrorException ex)
{
Console.WriteLine($"API error {ex.StatusCode}: {ex.ErrorMessage}");
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"State error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}DeleteAsync
Asynchronously deletes the preview token associated with the specified delivery token.
This method does not require a separate preview token UID. It deletes the preview token associated with the deliveryTokenUid passed to the class constructor.
Warning: This operation is irreversible. Deleted preview tokens cannot be recovered. Verify the delivery token UID before calling this method.
Validation
Throws InvalidOperationException with message "You are not logged in. Log in and try again." if the client has no active auth session.
For authentication and rate-limit errors (401, 403, 429), see Class-Level Notes.
| HTTP Status | Cause | Fix |
|---|---|---|
| 404 | The deliveryTokenUid does not exist or has no associated preview token. | Verify the delivery token UID before calling DeleteAsync(). |
Behavior
Executes the HTTP DELETE asynchronously and does not block the calling thread. This operation is irreversible. See Class-Level Notes, Async Methods for await and exception behavior.
| Name | Type | Description |
|---|---|---|
| collection | ParameterCollection | Optional query parameters appended to the request. See Class-Level Notes. |
Basic usage — asynchronously delete the preview token for a delivery token
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
// Initialize the client
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
// Await the async delete call
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.DeleteAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}All parameters — async with a ParameterCollection
using Contentstack.Management.Core;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
using Contentstack.Management.Core.Queryable;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ParameterCollection collection = new ParameterCollection();
collection.Add("include_branch", true);
try
{
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.DeleteAsync(collection);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}Error handling — catching async exceptions on a missing preview token
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Token;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
try
{
// Always await directly to surface exceptions correctly
ContentstackResponse contentstackResponse = await client
.Stack("<API_KEY>")
.PreviewToken("<DELIVERY_TOKEN_UID>")
.DeleteAsync();
}
catch (ContentstackErrorException ex)
{
Console.WriteLine($"API error {ex.StatusCode}: {ex.ErrorMessage}");
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"State error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}