You can use the Data API to programmatically interact with your 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 tokenThe Data API requires your database’s API endpoint and an application token with sufficient permissions to perform the requested operations.
In the Astra Portal, click the name of the Serverless (Vector) database that you want to use with the Data API.
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.
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 clientUpdate 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
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'
}
In general, all scripts that use a client will do the following:
Here’s an example of a simple client script. For a more detailed demo, see the quickstart for collections and the quickstart for tables.
from astrapy import DataAPIClient
# Instantiate the client
client = DataAPIClient()
# Connect to a database
database = client.get_database("API_ENDPOINT", token="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("API_ENDPOINT", {
token: "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);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.options.CollectionFindOneOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Instantiate the client
DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());
// Connect to a database
Database database =
client.getDatabase(
"API_ENDPOINT",
new DatabaseOptions("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 Databases reference.
Use HTTPYou 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 SummaryAPI_ENDPOINT
A database API endpoint URL. For information about how to find the endpoint, see Get endpoint and token.
KEYSPACE_NAME
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 default_keyspace
.
COLLECTION_NAME
or TABLE_NAME
The name of the collection or table where you want to run the command.
APPLICATION_TOKEN
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 "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: 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" }
}
}'
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4