Get started with the Data API (Java)

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 wide and 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 svc and look for the "PORT(S)" value that corresponds to NodePort.

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 com.datastax.astra.client.core.auth.UsernamePasswordTokenProvider;

public class Example {
  public static void main(String[] args) {
    String token =
        new UsernamePasswordTokenProvider("USERNAME", "PASSWORD").getTokenAsString();
  }
}

You can also inherently generate a token when you instantiate the client:

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;

public class Example {
  public static void main(String[] args) {
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
  }
}

Use a client

DataStax provides several clients, including the Java client, to facilitate interaction with the Data API.

Install the client

  • Maven

  • Gradle

  1. Update to Java version 17 or later if needed. DataStax recommends Java 21.

  2. Update to Apache Maven™ version 3.9 or later if needed.

  3. 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>
  1. Update to Java version 17 or later if needed. DataStax recommends Java 21.

  2. Update to Gradle version 11 or later if needed.

  3. 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:

  1. Instantiate a DataAPIClient object.

    All Data API interactions through a client start with a DataAPIClient object.

  2. Connect to a database.

    Most Data API interactions through a client require you to connect to a database through a DataAPIClient object.

  3. 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 com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
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.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 java.util.Optional;

public class Example {

  public static void main(String[] args) {
    // Instantiate the client
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");

    // Connect to a database
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // 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.vector(new float[] {0.08f, -0.62f, 0.39f}));
    Optional<Document> result = collection.findOne(filter, options);
    System.out.println(result);
  }
}

Use HTTP

Instead of using a client, you can make direct HTTP requests.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM