Connect with the TypeScript client

You can connect to your Serverless (Vector) databases with the astra-db-ts client.

For a guided experience to get started with the TypeScript client, see the Quickstart.

Install the TypeScript client

Install the TypeScript client:

  1. Verify that Node is version 18 or later:

    node --version
  2. Install astra-db-ts Latest astra-db-ts release on GitHub with your preferred package manager:

    • npm

    • Yarn

    • pnpm

    npm install @datastax/astra-db-ts
    yarn add @datastax/astra-db-ts
    pnpm add @datastax/astra-db-ts

Upgrade the TypeScript client

When a new client version is released, upgrade your client to get the latest features, improvements, and bug fixes. For information about major changes in specific client versions, see Data API client upgrade guide.

Reinstall the TypeScript client at the latest version.

For the TypeScript client, DataStax recommends reinstallation instead of upgrade and update commands, such as npm update @datastax/astra-db-ts, that install the latest version allowed by your package.json. If your package.json pins a version that is earlier than the actual latest version, then upgrade and update won’t install the actual latest version.

# npm
npm install @datastax/astra-db-ts@latest

# Yarn
yarn add @datastax/astra-db-ts@latest

# pnpm
pnpm add @datastax/astra-db-ts@latest

Set environment variables

The Data API requires your database’s API endpoint and application tokens with sufficient permissions to perform the requested operations.

Store application tokens and API endpoints in environment variables to simplify reuse in your scripts.

  1. In the Astra Portal navigation menu, select your Serverless (Vector) database.

  2. On the Overview tab, in the Database Details section, click Generate Token.

    The generated token has a custom Database Administrator role that is scoped to this database only. For more information, see Generate an application token for a database and Custom roles.

  3. Copy the token and store it securely. The Astra Portal shows the token only once.

  4. In the Database Details section, copy your database’s Data API endpoint.

    The Data API endpoint format is https://DATABASE_ID-REGION.apps.astra.datastax.com. If you aren’t using a client, be aware that DevOps API calls use a different endpoint format.

  5. (Optional) Create application tokens with other roles.

    The Database Administrator role authorizes operations within a database, but you might need a broader or narrower role for other operations, for example:

    • For operations above the database level, such as creating databases or keyspaces, you need an application token with the Organization Administrator role.

    • For production applications that only read data from a database, consider using an application token with a narrower scope, such as the Read Only User role.

  6. Set environment variables for your application tokens and Data API endpoint.

    • Linux or macOS

    • Windows

    export ASTRA_DB_API_ENDPOINT=API_ENDPOINT
    export ASTRA_DB_APPLICATION_TOKEN=TOKEN
    set ASTRA_DB_API_ENDPOINT=API_ENDPOINT
    set ASTRA_DB_APPLICATION_TOKEN=TOKEN

Connect to a Serverless (Vector) database

When you use a Data API client, your main entry point is the DataAPIClient object. Then, you get a database object to connect to a database. These operations are the basis of your client scripts. For more information, see Instantiate a client object and Connect to a database.

import { DataAPIClient, VectorDoc, UUID } from '@datastax/astra-db-ts';

const { ASTRA_DB_APPLICATION_TOKEN, ASTRA_DB_API_ENDPOINT } = process.env;

// Initialize the client and get a "Db" object
const client = new DataAPIClient(ASTRA_DB_APPLICATION_TOKEN);
const db = client.db(ASTRA_DB_API_ENDPOINT);

console.log(`* Connected to DB ${db.id}`);

Next steps

After you connect to a database, you can extend your script to work with the collections and documents in it. You can also use the TypeScript client to manage databases and keyspaces.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

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: +1 (650) 389-6000, info@datastax.com