Get a database object (Go)

When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.

Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API (Go).

For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.

Result

Returns a Db struct that you can use to perform CRUD operations on your database

Parameters

Use the Database method, which belongs to the DataAPIClient type.

Method signature
func (c *DataAPIClient) Database(
  endpoint string,
  opts ...options.APIOption
) *Db
Name Type Summary

endpoint

string

A database API endpoint URL.

opts

…​options.APIOption

Optional. A specification of options to override the inherited defaults. Use this to customize the interaction of the client with the admin object. For example, you can change the default timeouts or token.

You can override these options in downstream Data API operations.

See Methods of the options.APIOption builder for more details.

Methods of the options.APIOption builder
Method Summary

SetToken(token string)

Optional.

An application token. For more information about how to generate a token, see Get endpoint and token.

The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations.

Alternatively, use SetUsernamePasswordTokenProvider() or SetTokenProvider() to set the token based on a username and password.

SetUsernamePasswordTokenProvider(user string, pass string)

Optional. Generates and sets the application token based on a username and password.

The username and password must be tied to a role that has sufficient permissions to perform the desired operations.

Alternatively, use SetToken() to pass a token directly.

SetTokenProvider(provider options.TokenProvider)

Optional. Generates and sets the application token based on a TokenProvider like NewUsernamePasswordTokenProvider.

The username and password set in the TokenProvider must be tied to a role that has sufficient permissions to perform the desired operations.

Alternatively, use the SetUsernamePasswordTokenProvider() shortcut, or use SetToken() to pass a token directly.

SetAstraEnvironment(v AstraEnvironment)

Optional. Specify the environment this client will work in.

Default: options.AstraEnvironmentProd.

SetGeneralMethodTimeout(v time.Duration)

Optional. The maximum time that the client should wait for an entire operation, which may involve multiple HTTP requests, to complete. Default: 30 seconds

SetRequestTimeout(v time.Duration)

Optional. The maximum time that the client should wait for individual HTTP requests.

Default: 15 seconds

SetKeyspaceAdminTimeout(v time.Duration)

Optional. The maximum time that the client should wait for keyspace administrative operations like CreateKeyspace and DropKeyspace.

Default: 30 seconds

SetCollectionAdminTimeout(v time.Duration)

Optional. The maximum time that the client should wait for collection administrative operations like CreateCollection and DropCollection.

Default: 60 seconds

SetTableAdminTimeout(v time.Duration)

Optional. The maximum time that the client should wait for table administrative operations like CreateTable, CreateIndex, and CreateType.

Default: 30 seconds

UpdateTimeout(v …​options.TimeoutOption)

Optional. An alternative to SetGeneralMethodTimeout(), SetRequestTimeout(), SetDatabaseAdminTimeout(), SetKeyspaceAdminTimeout(), SetCollectionAdminTimeout(), and SetTableAdminTimeout().

SetEnvironment(v options.Environment)

Sets the database backend.

You must set this to options.EnvironmentHCD

SetKeyspace(v string)

Optional. Specify the working keyspace. This keyspace is used for all downstream operations unless you later override it with a method such as UseKeyspace, Collection, CreateCollection, Table, or CreateTable.

If you don’t set this, you must specify the keyspace in a downstream method before performing an operation that requires a keyspace.

AddHeader(key string, value string)

Optional. Specifies headers for all HTTP requests to the Data API.

AddCaller(name string, version string)

Optional. Attaches special identifying entries to the User-Agent header for all HTTP requests to the Data API.

SetAPIVersion(v string)

Optional. Sets the Data API version.

Default: v1

SetHTTPClient(v *http.Client)

Optional. Sets the HTTP client to use for requests.

SetWarningHandler(handler options.WarningHandler)

Optional. Specifies a callback function that is invoked for warnings emitted by the Data API. Data API warnings indicate non-fatal conditions that don’t prevent the operation from completing, such as missing indexes or deprecated features.

UpdateSerdes(v …​options.SerdesOption)

Optional. Sets the serialization/deserialization options.

Examples

The following examples demonstrate how to connect to a database.

package main

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

func main() {

	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	_ = client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			),
	)
}

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