Instantiate a client object (C#)
All Data API interactions through a client start with a DataAPIClient object.
Once you have a DataAPIClient object, you can connect to a database and the perform CRUD operations on your data.
Creating a client object is an essential component of Data API client scripts. For more information, see Get started with the Data API (C#).
For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Result
Returns a DataAPIClient object that you can use to interact with the Data API.
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
Optional. An application token.
You can use The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations. For more information about how to generate a token, see Get endpoint and token.
|
|
A class wrapping the advanced configuration of the client, such as |
Examples
The following examples demonstrate how to instantiate a client.
Instantiate a client
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
}
}
Instantiate a client and pass a token
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
var client = new DataAPIClient(
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
}
}
Instantiate a client and specify options
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
var options = new CommandOptions()
{
TimeoutOptions = new TimeoutOptions()
{
ConnectionTimeout = TimeSpan.FromMilliseconds(3000),
RequestTimeout = TimeSpan.FromMilliseconds(9000),
},
Destination = DataAPIDestination.HCD,
};
var client = new DataAPIClient(options);
}
}
Errors
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see C# client error handling.
Client reference
For more information, see the client reference.