Get started with the Data API (TypeScript)
You can use the Data API to programmatically interact with your databases.
The Data API provides an entry point for application development with Hyper-Converged Database (HCD) databases, including a variety of generative AI ecosystem integrations. It leverages the scalability, performance, and real-time indexing capabilities of Apache Cassandra® to support generative AI application development.
If you are new to the Data API, check out the quickstart for collections or the quickstart for tables for a demo of some common commands.
Get endpoint and token
The Data API requires your database’s API endpoint and an application token.
Get endpoint
The Data API endpoint for your database has the form: http://CLUSTER_HOST:GATEWAY_PORT
-
Replace CLUSTER_HOST with the external IP address of any node in your cluster. To find this, run
kubectl get nodes -o wideand use any of the values listed under "EXTERNAL-IP" in the output. -
Replace GATEWAY_PORT with the port number for your API gateway service. To find this, run
kubectl get svcand look for the "PORT(S)" value that corresponds toNodePort.
Generate token
The Data API requires an application token in the following format: Cassandra:BASE64-ENCODED_USERNAME:BASE64_ENCODED_PASSWORD.
The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations.
You set a username and password when you create a cluster.
If you didn’t provide superuser credentials when you created your cluster, they were generated automatically and saved in a superuser secret named CLUSTER_NAME-superuser.
The CLUSTER_NAME-superuser secret contains both the username and the password.
You can use UsernamePasswordTokenProvider from the client to generate the token.
Alternatively, you can build the token yourself.
import { UsernamePasswordTokenProvider } from "@datastax/astra-db-ts";
const token = new UsernamePasswordTokenProvider("USERNAME", "PASSWORD");
Use a client
DataStax provides several clients, including the TypeScript client, to facilitate interaction with the Data API.
Install the client
-
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
Use the client to interact with data
In general, all scripts that use a client will do the following:
-
Instantiate a
DataAPIClientobject.All Data API interactions through a client start with a
DataAPIClientobject. -
Most Data API interactions through a client require you to connect to a database through a
DataAPIClientobject. -
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.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Instantiate the client
const client = new DataAPIClient({ environment: "hcd" });
// Connect to a database
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
// 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: { $vector: [0.08, -0.62, 0.39] } },
);
console.log(result);
})();
Use HTTP
Instead of using a client, you can make direct HTTP requests.