Get a collection (Go)

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 object that corresponds to the specified collection name.

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

Parameters

Use the Collection method, which belongs to the Db type.

Method signature
func (d *Db) Collection(
  name string,
  opts ...options.GetCollectionOption
) *Collection
Name Type Summary

name

string

The name of the collection to get.

opts

GetCollectionOption

Optional. The options for this method.

Methods of the GetCollectionOption builder
Method Summary

SetKeyspace(v 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.

SetEmbeddingAPIKey(v 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, you must use SetEmbeddingHeadersProvider(options.NewAWSEmbeddingHeadersProvider(accessKeyID string, secretAccessKey string)) instead of SetEmbeddingAPIKey(apiKey string) to pass your access ID and secret ID.

UpdateAPIOptions(v …​APIOption)

Optional. General API options for this operation, including the timeout.

Examples

The following examples demonstrate how to get a collection.

Get a collection

package main

import (
	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	// Get a database
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	// Get a collection
	database.Collection("COLLECTION_NAME")
}

Get a collection and specify the keyspace

package main

import (
	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	// Get a database
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	// Get a collection
	database.Collection(
		"COLLECTION_NAME",
		options.GetCollection().SetKeyspace("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