Get a database object
When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.
Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API.
For more information about the Data API client hierarchy, see Python client usage, TypeScript client usage, and Java client usage.
Result
-
Python
-
TypeScript
-
Java
-
C#
Returns a Database object that you can use to perform CRUD operations on your database
Returns a Db object that you can use to perform CRUD operations on your database
Returns a Database object that you can use to perform CRUD operations on your database
Returns a Database object that you can use to perform CRUD operations on your database
Parameters
-
Python
-
TypeScript
-
Java
-
C#
Use the get_database method, which belongs to the astrapy.DataAPIClient class.
Method signature
get_database(
api_endpoint: str,
*,
token: str | TokenProvider,
keyspace: str,
spawn_api_options: APIOptions,
) -> Database
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
|
Optional. An application token, or an instance of The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Specify the working keyspace.
You can override this in downstream methods such as |
|
|
Optional. A complete or partial specification of the APIOptions. This overrides the defaults inherited from the Data API client. This allows you to customize the interaction with the Data API. For example, you can use this parameter to change the timeout settings. |
Use the db method, which belongs to the DataAPIClient class.
Method signature
db(
endpoint: string,
options?: {
logging?: LoggingConfig,
keyspace?: string | null,
token?: string | TokenProvider | null,
dataApiPath?: string,
serdes?: DbSerDesConfig,
timeoutDefaults?: TimeoutDescriptor,
}
): Db
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
Optional.
The options to use for the database.
See Properties of |
| Name | Type | Summary |
|---|---|---|
|
Specify the working keyspace.
You can override this in downstream methods such as |
|
|
Optional.
Whether to monitor commands through Default: false. |
|
|
Optional. An application token, or an instance of The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional. The path to the Data API. Default: |
Use the getDatabase method, which belongs to the com.datastax.astra.client.DataAPIClient class.
Method signature
public Database getDatabase(String apiEndpoint, DatabaseOptions dbOptions)
public Database getDatabase(String apiEndpoint, String keyspace)
public Database getDatabase(String apiEndpoint)
public Database getDatabase(UUID databaseId)
public Database getDatabase(UUID databaseId, DatabaseOptions dbOptions)
public Database getDatabase(UUID databaseId, String region, DatabaseOptions dbOptions)
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
|
A database ID. You can optionally specify a region for multi-region databases. DataStax recommends using |
|
|
Specify the working keyspace.
You can override this in downstream methods such as |
|
The options to use for the database. |
Use the GetDatabase method, which belongs to the DataAPIClient class.
Method signature
public Database GetDatabase(string apiEndpoint);
public Database GetDatabase(
string apiEndpoint, string token, string keyspace = null
);
public Database GetDatabase(
string apiEndpoint, GetDatabaseOptions options
);
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL. |
|
|
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. If you specified a token when instantiating the |
|
|
Specify the working keyspace.
You can override this in downstream methods such as |
|
Optional.
General API options for this operation, including the keyspace and timeout.
Keyspace is required unless you specified a keyspace when instantiating the |
Examples
The following examples demonstrate how to connect to a database.
-
Python
-
TypeScript
-
Java
-
C#
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
}
}
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 }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
)
);
}
}
Client reference
-
Python
-
TypeScript
-
Java
-
C#
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.