Get started with the Data API
You can use the Data API to programmatically interact with your Serverless (Vector) databases.
The Data API provides an entry point for application development with Astra DB Serverless databases, including a variety of GenAI ecosystem integrations like LangChain, LlamaIndex, and embedding providers. It leverages the scalability, performance, and real-time indexing capabilities of Apache Cassandra® to support GenAI application development.
The Data API only supports Serverless (Vector) databases. It doesn’t support Serverless (Non-Vector) databases.
Get endpoint and token
The Data API requires your database’s API endpoint and an application token with sufficient permissions to perform the requested operations.
-
In the Astra Portal navigation menu, click Databases, and then click the name of your Serverless (Vector) database.
-
On the Overview tab, in the Database Details section, copy your database’s Data API endpoint.
The endpoint format is
https://DATABASE_ID-REGION.apps.astra.datastax.com
. -
On the Overview tab, in the Database Details section, click Generate Token, then copy the token.
The generated token has a custom Database Administrator role that is scoped to this database only.
Optionally, you can create application tokens with other roles.
Use a client
DataStax provides several clients to facilitate interaction with the Data API.
If there is no client for your preferred language, see Use HTTP.
Install a client
-
Python
-
TypeScript
-
Java
-
Update to Python version 3.8 or later if needed.
-
Update to pip version 23.0 or later if needed.
-
Install the latest version of the astrapy package
.
pip install "astrapy>=2.0,<3.0"
-
Update to Node version 18 or later if needed.
-
Update to TypeScript version 5 or later if needed. This is unnecessary if you are using JavaScript instead of TypeScript.
-
Install the latest version of the @datastax/astra-db-ts package
.
For example:
npm install @datastax/astra-db-ts
-
Maven
-
Gradle
-
Update to Java version 17 or later if needed. DataStax recommends Java 21.
-
Update to Maven version 3.9 or later if needed.
-
Add a dependency to the latest version of the astra-db-java package
.
pom.xml<dependencies> <dependency> <groupId>com.datastax.astra</groupId> <artifactId>astra-db-java</artifactId> <version>VERSION</version> </dependency> </dependencies>
-
Update to Java version 17 or later if needed. DataStax recommends Java 21.
-
Update to Gradle version 11 or later if needed.
-
Add a dependency to the latest version of the astra-db-java package
.
build.gradle(.kts)dependencies { implementation 'com.datastax.astra:astra-db-java:VERSION' }
Use the client to interact with data
In general, all scripts that use a client will do the following:
-
Instantiate a
DataAPIClient
object.All Data API interactions through a client start with a
DataAPIClient
object. -
Most Data API interactions through a client require you to connect to a database through a
DataAPIClient
object. -
Perform CRUD operations.
For example, you can create a collection or table, insert data, and find data. For a full list of operations, see the collection commands and table commands.
Here’s an example of a simple client script. For a more detailed demo, see the quickstart for collections and the quickstart for tables.
-
Python
-
TypeScript
-
Java
from astrapy import DataAPIClient
# Instantiate the client
client = DataAPIClient()
# Connect to a database
database = client.get_database("ASTRA_DB_API_ENDPOINT", token="ASTRA_DB_APPLICATION_TOKEN")
# Get an existing collection
collection = database.get_collection("COLLECTION_NAME")
# Use vector search and filters to find a document
result = collection.find_one(
{
"$and": [
{"is_checked_out": False},
{"number_of_pages": {"$lt": 300}},
]
},
sort={"$vectorize": "A thrilling story set in a futuristic world"}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Instantiate the client
const client = new DataAPIClient();
// Connect to a database
const database = client.db('ASTRA_DB_API_ENDPOINT', {
token: 'ASTRA_DB_APPLICATION_TOKEN',
});
// Get an existing collection
const collection = database.collection('COLLECTION_NAME');
// Use vector search and filters to find a document
(async function () {
const result = await collection.findOne(
{
$and: [{ is_checked_out: false }, { number_of_pages: { $lt: 300 } }],
},
{ sort: { $vectorize: "A thrilling story set in a futuristic world" } }
);
console.log(result);
})();
package com.examples;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.collections.commands.options.CollectionFindOneOptions;
import com.datastax.astra.client.core.query.Sort;
import java.util.Optional;
public class FindOne {
public static void main(String[] args) {
// Instantiate the client
DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());
// Connect to a database
Database database =
client.getDatabase(
"ASTRA_DB_API_ENDPOINT", new DatabaseOptions("ASTRA_DB_APPLICATION_TOKEN", new DataAPIClientOptions()));
// Get an existing collection
Collection<Document> collection = database.getCollection("COLLECTION_NAME");
// Use vector search and filters to find a document
Filter filter = Filters.and(
Filters.eq("is_checked_out", false),
Filters.lt("number_of_pages", 300));
CollectionFindOneOptions options = new CollectionFindOneOptions()
.sort(Sort.vectorize("A thrilling story set in a futuristic world"));
Optional<Document> result = collection.findOne(filter, options);
System.out.println(result);
}
}
You can also use the clients for database administration, such as creating databases and keyspaces. For more information, see Get a database object.
Use HTTP
You can interact directly with the Data API over HTTP using tools like curl or Go’s net/http
package.
Data API HTTP requests always use the POST
method, regardless of the actual CRUD operation performed by the command.
For a full list of commands, see the collection commands and table commands.
Most endpoints require you specify values:
Name | Summary |
---|---|
|
A database API endpoint URL, such as |
|
The target keyspace where you want to run the command.
The target keyspace is also known as the working keyspace.
All Serverless (Vector) databases have an initial default keyspace called |
|
The name of the collection or table where you want to run the command. |
|
An application token with sufficient permissions to perform the requested command on the specified database. |
For example, use vector search and filters to find a document:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]},
"sort": { "$vectorize": "A thrilling story set in a futuristic world" }
}
}'