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 internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Result
-
Python
-
TypeScript
-
Go
-
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
|
The Go client is in preview. For more information, see astra-db-go. |
Returns a Db struct 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
-
Go
-
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, such as |
|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
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, such as |
|
Optional.
The options to use for the database.
See Properties of |
| Name | Type | Summary |
|---|---|---|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
Optional.
Whether to monitor commands through Default: false. |
|
|
Optional.
An application token for a database.
Tokens are prefixed with The token should have sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional. The path to the Data API. Default: |
|
The Go client is in preview. For more information, see astra-db-go. |
Use the Database method, which belongs to the DataAPIClient type.
Method signature
func (c *DataAPIClient) Database(
endpoint string,
opts ...options.APIOption
) *Db
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL, such as |
|
Optional. A specification of options to override the inherited defaults. Use this to customize the interaction of the client with the admin object. For example, you can change the default timeouts or token. You can override these options in downstream Data API operations. See Methods of the |
| Method | Summary |
|---|---|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. |
|
Optional. Specify the environment this client will work in. Default: |
|
Optional. The maximum time that the client should wait for an entire operation, which may involve multiple HTTP requests, to complete. Default: 30 seconds |
|
Optional. The maximum time that the client should wait for individual HTTP requests. Default: 15 seconds |
|
Optional.
The maximum time that the client should wait for database administrative operations like Default: 10 minutes |
|
Optional.
The maximum time that the client should wait for keyspace administrative operations like Default: 30 seconds |
|
Optional.
The maximum time that the client should wait for collection administrative operations like Default: 60 seconds |
|
Optional.
The maximum time that the client should wait for table administrative operations like Default: 30 seconds |
|
Optional.
An alternative to |
|
Optional. Sets the database backend. Default: |
|
Optional The keyspace to use for downstream operations. Default: default_keyspace |
|
Optional. Specifies headers for all HTTP requests to the Data API. |
|
Optional. Attaches special identifying entries to the User-Agent header for all HTTP requests to the Data API. |
|
Optional. Sets the Data API version. Default: v1 |
|
Optional. Sets the HTTP client to use for requests. |
|
Optional. Specifies a callback function that is invoked for warnings emitted by the Data API. Data API warnings indicate non-fatal conditions that don’t prevent the operation from completing, such as missing indexes or deprecated features. |
|
Optional. Sets the serialization/deserialization options. |
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, such as |
|
|
A database ID. You can optionally specify a region for multi-region databases. DataStax recommends using |
|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
The region to use for connecting to the database. The database must be deployed in that region. If you don’t set this parameter and the region cannot be inferred from an API endpoint, an additional request is made to determine the default region and use it in subsequent operations. |
|
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, such as |
|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
Optional. General API options for this operation, including the keyspace and timeout. For more information and examples, see Customize API interaction. |
Examples
The following examples demonstrate how to connect to a database.
-
Python
-
TypeScript
-
Go
-
Java
-
C#
from astrapy import DataAPIClient
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
import { DataAPIClient } from "@datastax/astra-db-ts";
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
|
The Go client is in preview. For more information, see astra-db-go. |
package main
import (
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
client := astra.NewClient()
_ = client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;
public class Example {
public static void main(String[] args) {
DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());
Database database =
client.getDatabase(
"API_ENDPOINT",
new DatabaseOptions("APPLICATION_TOKEN", new DataAPIClientOptions()));
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
}
}
Client reference
-
Python
-
TypeScript
-
Go
-
Java
-
C#
For more information, see the client reference.
For more information, see the client reference.
|
The Go client is in preview. For more information, see astra-db-go. |
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.