Start with the Data API
You can interact with the Data API directly or through the clients. Clients are available for Python, TypeScript, and Java.
Prerequisites
Review the prerequisites and other information about the available APIs in the API reference overview.
If you plan to use a Data API client, and you haven’t done so already, install the client for your preferred language:
Connect to the database
When you create apps using the Data API clients for Python, TypeScript, and Java, your main entry point is the DataAPIClient
object.
Using an authentication token, the client can produce a reference to a database.
For self-managed databases, you can specify the authentication token using the UsernamePasswordTokenProvider
class.
-
Python
-
TypeScript
-
Java
View this topic in more detail on the API Reference.
token_provider = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD)
# Initialize the client and get a "Database" object
database = DataAPIClient(
token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)
Returns:
database
- An instance of the Database
class.
Example response
Database(api_endpoint="http://localhost:8181", token="Cassandra:dX...", namespace="(not set)")
Parameters for UsernamePasswordTokenProvider
:
Name | Type | Summary |
---|---|---|
username |
|
A username for token creation. Example: |
password |
|
A password for token creation. Example: |
Parameters for DataAPIClient
:
Name | Type | Summary |
---|---|---|
token |
|
The authentication token. It can be a string or an instance of an |
environment |
|
The target Data API environment. Accepted values are grouped into |
Parameters for get_database
:
Name | Type | Summary |
---|---|---|
api_endpoint |
|
The URL for the API endpoint base URL, such as |
token |
|
A specialized token, which overrides — if provided — the one previously given to the client. |
namespace |
|
Pass this argument to set a working namespace for the returned |
Example:
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Database settings
DB_USERNAME = "cassandra"
DB_PASSWORD = "cassandra"
DB_API_ENDPOINT = "http://localhost:8181"
# Prepare a token provider
token_provider = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD)
# Initialize the client and get a "Database" object
client = DataAPIClient(token=token_provider, environment=Environment.HCD)
database = client.get_database(DB_API_ENDPOINT)
Most
See the API reference for |
View this topic in more detail on the API Reference.
// Build a token in the required format
const tp = new UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD);
// Initialize the client and get a "Db" object
const client = new DataAPIClient({ environment: 'hcd' });
Returns:
DataAPIClient
- An instance of the client class.
Example response
DB_USERNAME = "cassandra" DB_PASSWORD = "cassandra" DB_ENVIRONMENT = "hcd" # Build a token tp = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD) # Initialize the client and get a "Database" object client = DataAPIClient(token=tp, environment=Environment.DB_ENVIRONMENT)
Parameters:
Name | Type | Summary |
---|---|---|
username |
|
A username for token creation. Example: |
password |
|
A password for token creation. Example: |
options? |
The options to use for the client, including defaults. |
Options (DataAPIClientOptions
):
Name | Type | Summary |
---|---|---|
Options related to the API requests the client makes. |
||
Allows default options for when spawning a Db instance. |
||
Allows default options for when spawning some Admin instance. |
Http options (DataAPIHttpOptions
):
The DataAPIHttpOptions
type is a discriminated
union on the client
field.
There are four available behaviors for the client
field:
-
httpOptions
not set: Usefetch-h2
if available or fall back tofetch
. -
client: 'default'
or unset: Usefetch-h2
if available or throw an error. -
client: 'fetch'
: Only use the nativefetch
API. -
client: 'custom'
: Pass a custom Fetcher implementation to the client.
fetch-h2
is generally available by default on node runtimes only.
On other runtimes, you might need to use the native fetch
API or, if your code is minified, pass in the fetch-h2
module manually.
For more information on http clients, see the astra-db-ts README and the API reference.
Monitoring/logging:
For information on setting up commands monitoring, see the astra-db-ts README.
Example:
import { DataAPIClient, UsernamePasswordTokenProvider, VectorDoc, UUID } from '@datastax/astra-db-ts';
// Database settings
const DB_USERNAME = "cassandra";
const DB_PASSWORD = "cassandra";
const DB_API_ENDPOINT = "http://localhost:8181";
const DB_ENVIRONMENT = "hcd";
const DB_NAMESPACE = "cycling";
// Database settings if you exported them as environment variables
// const DB_USERNAME = process.env.DB_USERNAME;
// const DB_PASSWORD = process.env.DB_PASSWORD;
// const DB_API_ENDPOINT = process.env.DB_API_ENDPOINT;
// OpenAI settings
const OPEN_AI_PROVIDER = "openai";
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
const MODEL_NAME = "text-embedding-3-small";
// Build a token in the required format
const tp = new UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD);
// Initialize the client and get a "Db" object
const client = new DataAPIClient({ environment: 'hcd' });
const db = client.db(DB_API_ENDPOINT, { token: tp });
const dbAdmin = db.admin({ environment: 'hcd' });
// Build a token in the form of Cassandra:base64(username):base64(password)
String token = new UsernamePasswordTokenProvider(cassandraUserName, cassandraPassword).getTokenAsString();
System.out.println("1/7 - Creating Token: " + token);
// Initialize the client
DataAPIClient client = new DataAPIClient(token, builder().withDestination(databaseEnvironment).build());
Returns:
DataAPIClient
- An instance of the client class.
Parameters:
Name | Type | Summary |
---|---|---|
username |
|
A username for token creation. Example: |
password |
|
A password for token creation. Example: |
options |
A class wrapping the advanced configuration of the client such as as |
Example:
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.Database;
import com.datastax.astra.client.admin.DataAPIDatabaseAdmin;
import com.datastax.astra.client.model.CollectionOptions;
import com.datastax.astra.client.model.CommandOptions;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.FindOneOptions;
import com.datastax.astra.client.model.NamespaceOptions;
import com.datastax.astra.client.model.SimilarityMetric;
import com.datastax.astra.internal.auth.UsernamePasswordTokenProvider;
import java.util.Optional;
import static com.datastax.astra.client.DataAPIClients.DEFAULT_ENDPOINT_LOCAL;
import static com.datastax.astra.client.DataAPIOptions.DataAPIDestination.HCD;
import static com.datastax.astra.client.DataAPIOptions.builder;
import static com.datastax.astra.client.model.Filters.eq;
public class QuickStartHCD {
public static void main(String[] args) {
// Database Settings
String cassandraUserName = "cassandra";
String cassandraPassword = "cassandra";
String dataApiUrl = DEFAULT_ENDPOINT_LOCAL; // http://localhost:8181
String databaseEnvironment = "HCD" // DSE, HCD, or ASTRA
String keyspaceName = "ks1";
String collectionName = "lyrics";
// Database settings if you export them as environment variables
// String cassandraUserName = System.getenv("DB_USERNAME");
// String cassandraPassword = System.getenv("DB_PASSWORD");
// String dataApiUrl = System.getenv("DB_API_ENDPOINT");
// OpenAI Embeddings
String openAiProvider = "openai";
String openAiKey = System.getenv("OPENAI_API_KEY"); // Need to export OPENAI_API_KEY
String openAiModel = "text-embedding-3-small";
int openAiEmbeddingDimension = 1536;
// Build a token in the form of Cassandra:base64(username):base64(password)
String token = new UsernamePasswordTokenProvider(cassandraUserName, cassandraPassword).getTokenAsString();
System.out.println("1/7 - Creating Token: " + token);
// Initialize the client
DataAPIClient client = new DataAPIClient(token, builder().withDestination(databaseEnvironment).build());
System.out.println("2/7 - Connected to Data API");