Get a collection (TypeScript)

Gets a reference to a collection for use with the Data API clients.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Returns a Collection<Schema> object that corresponds to the specified collection name.

This method returns a Collection object even for collections that don’t exist.

A Collection is typed as Collection<Schema>, where Schema defaults to SomeDoc (Record<string, any>). Providing the specific Schema type enables stronger typing for collection operations. For more information, see Typing Collections and Tables.

Parameters

Use the collection method, which belongs to the Db class.

Method signature
collection <Schema extends SomeDoc = SomeDoc>(
  name: string,
  options?: {
    keyspace?: string,
    logging?: DataAPILoggingConfig,
    serdes?: CollectionSerDesConfig,
    embeddingApiKey?: string | EmbeddingHeadersProvider,
    timeoutDefaults?: TimeoutDescriptor,
  }
): Collection<Schema>
Name Type Summary

name

string

The name of the collection.

options

CollectionOptions

Optional. The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

embeddingApiKey

string

Optional. This only applies to collections with a vectorize embedding provider integration.

Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS.

The API key is sent to the Data API for every operation on the collection. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize.

If you use an AWS embedding provider, the embeddingApiKey option must instead use the AWSEmbeddingHeadersProvider class to pass your access ID and secret ID.

keyspace

string

Optional. The keyspace that contains the collection.

For an example, see Get a collection and specify the keyspace.

Default: The working keyspace for the database. This is default_keyspace unless you set a different working keyspace when you created the Db object.

logging

string

Optional. The configuration for logging events emitted by the DataAPIClient.

serdes

string

Optional. The configuration for serialization/deserialization by the DataAPIClient.

For more information, see Custom Ser/Des.

timeoutDefaults

TimeoutDescriptor

Optional.

The default timeout(s) to apply to operations performed on this Collection instance. You can specify requestTimeoutMs, generalMethodTimeoutMs, and collectionAdminTimeoutMs.

For more information about the TimeoutDescriptor, see TypeScript client internals: TimeoutDescriptor.

Examples

The following examples demonstrate how to get a collection.

Get a collection

  • Typed collections

  • Untyped collections

You can manually define a client-side type for your collection to help statically catch errors.

import { DataAPIClient } from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the type for the collection
interface User {
  name: string;
  age?: number;
}

// Get a collection
(async function () {
  const collection = await database.collection<User>("COLLECTION_NAME");
})();

If you don’t pass a type parameter, the collection remains untyped. This is a more flexible but less type-safe option.

import { DataAPIClient } from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Get a collection
(async function () {
  const collection = await database.collection("COLLECTION_NAME");
})();

Get a collection and specify the keyspace

  • Typed collections

  • Untyped collections

You can manually define a client-side type for your collection to help statically catch errors.

import { DataAPIClient } from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the type for the collection
interface User {
  name: string;
  age?: number;
}

// Get a collection
(async function () {
  const collection = await database.collection<User>("COLLECTION_NAME", {
    keyspace: "KEYSPACE_NAME",
  });
})();

If you don’t pass a type parameter, the collection remains untyped. This is a more flexible but less type-safe option.

import { DataAPIClient } from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Get a collection
(async function () {
  const collection = await database.collection("COLLECTION_NAME", {
    keyspace: "KEYSPACE_NAME",
  });
})();

Client reference

For more information, see the client reference.

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