Create a keyspace

Creates a new keyspace in a database.

Keyspaces are used to store collections and tables in Hyper-Converged Database (HCD).

Result

  • Python

  • TypeScript

  • Java

  • curl

Creates the specified keyspace.

Does not return anything.

Creates the specified keyspace.

Returns a promise that resolves once the operation completes.

Creates the specified keyspace.

Does not return anything.

Creates the specified keyspace.

If the command succeeds, the response indicates the success.

Example successful response:

{
  "status": {
    "ok": 1
  }
}

Parameters

  • Python

  • TypeScript

  • Java

  • curl

Use the create_keyspace method, which belongs to the astrapy.DataAPIDatabaseAdmin class.

Method signature
create_keyspace(
  name: str,
  *,
  replication_options: dict[str, Any],
  update_db_keyspace: bool,
  keyspace_admin_timeout_ms: int,
  request_timeout_ms: int,
  timeout_ms: int,
  **kwargs: Any,
) -> None
Name Type Summary

name

str

The name of the keyspace to create.

If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds.

replication_options

dict[str, Any]

Optional. Specifies the options for keyspace replication across database nodes.

The structure must be similar to: {"class": "SimpleStrategy", "replication_factor": 1}.

update_db_keyspace

bool

Optional. Whether to set the working keyspace of the Database or AsyncDatabase object that created the current DataAPIDatabaseAdmin object to the new keyspace.

keyspace_admin_timeout_ms

int

Optional. A timeout, in milliseconds, to impose on the underlying API request. If not provided, the DataAPIDatabaseAdmin defaults apply.

This parameter is aliased as request_timeout_ms and timeout_ms for convenience.

Use the createKeyspace method, which belongs to the DataAPIDbAdmin class.

Method signature
async createKeyspace(
  keyspace: string,
  options?: {
    replication?: KeyspaceReplicationOptions,
    updateDbKeyspace?: boolean,
    timeout?: number | TimeoutDescriptor,
  }
): void
Name Type Summary

keyspace

string

The name of the keyspace to create.

If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds.

options

CreateDataAPIKeyspaceOptions

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

Properties of options
Name Type Summary

replication

KeyspaceReplicationOptions

Optional. Specifies the options for keyspace replication across database nodes.

Default: {class: 'SimpleStrategy', replicationFactor: 1}

updateDbKeyspace

boolean

Optional. Whether to set the working keyspace of the Db object that created the current DbAdmin object to the new keyspace.

timeout

number | TimeoutDescriptor

Optional.

The timeout(s) to apply to this method. You can specify requestTimeoutMs and keyspaceAdminTimeoutMs.

Details about the timeout parameter

The TimeoutDescriptor object can contain these properties:

  • requestTimeoutMs (number): The maximum time, in milliseconds, that the client should wait for each underlying HTTP request.

    Default: The default value for the admin object. This default is 10 seconds unless you specified a different default when you initialized the DataAPIDbAdmin or DataAPIClient object.

  • keyspaceAdminTimeoutMs (number): The maximum time, in milliseconds, for keyspace admin operations like creating, dropping, and listing keyspaces.

    Default: The default value for the admin object. This default is 30 seconds unless you specified a different default when you initialized the DataAPIDbAdmin or DataAPIClient object.

If you specify a number instead of a TimeoutDescriptor object, that number will be applied to both requestTimeoutMs and keyspaceAdminTimeoutMs.

Use the createKeyspace method, which belongs to the com.datastax.astra.client.admin.DatabaseAdmin class.

Method signature
void createKeyspace(String keyspaceName)
void createKeyspace(KeyspaceDefinition keyspaceDefinition)
void createKeyspace(
  KeyspaceDefinition keyspaceDefinition,
  CreateKeyspaceOptions options
)
Name Type Summary

keyspaceName

String

The name of the keyspace to create.

keyspaceDefinition

KeyspaceDefinition

The keyspace name and optionally replication factors. See Methods of KeyspaceDefinition for more details.

options

CreateKeyspaceOptions

Optional. The options for this operation. See Methods of CreateKeyspaceOptions for more details.

Methods of KeyspaceDefinition
Method Parameters Summary

name()

String

The name of the keyspace to create.

simpleStrategy()

int

Specifies the options for keyspace replication across database nodes using a simple strategy.

networkTopologyStrategy()

Map<String, Integer>

Specifies the options for keyspace replication across database nodes using a network topology strategy.

Methods of CreateKeyspaceOptions
Method Parameters Summary

updateDBKeyspace()

boolean

Optional. Whether to set the working keyspace of the Database object that created the current DatabaseAdmin object to the new keyspace.

Default: false

ifNotExists()

boolean

Optional. Whether the command should silently succeed even if a keyspace with the given name already exists and no new keyspace was created.

Default: true

Use the createKeyspace command.

Command signature
curl -sS -L -X POST "API_ENDPOINT/v1" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createKeyspace": {
    "name": "KEYSPACE_NAME",
    "options": {
      "replication": REPLICATION_STRATEGY,
    }
  }
}'
Name Type Summary

name

string

The name of the keyspace to create.

If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds.

options.replication

object

Optional. Specifies the options for keyspace replication across database nodes.

The structure must be similar to: {"class": "SimpleStrategy", "replication_factor": 1}.

Examples

The following examples demonstrate how to create a keyspace.

Create a keyspace

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)

# Get an admin object
admin = database.get_database_admin()

# Create a keyspace
admin.create_keyspace("KEYSPACE_NAME")
import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});

// Get an admin object
const admin = database.admin({ environment: "hcd" });

// Create a keyspace
(async function () {
  await admin.createKeyspace("KEYSPACE_NAME");
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;

public class Example {

  public static void main(String[] args) {
    // Get a database
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Get an admin object
    DatabaseAdmin admin = database.getDatabaseAdmin();

    // Create a keyspace
    admin.createKeyspace("KEYSPACE_NAME");
  }
}
curl -sS -L -X POST "API_ENDPOINT/v1" \
  --header "Token: APPLICATION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "createKeyspace": {
    "name": "KEYSPACE_NAME"
  }
}'

Create a keyspace and set it as the working keyspace for the database

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)

# Get an admin object
admin = database.get_database_admin()

# Create a keyspace
admin.create_keyspace("KEYSPACE_NAME", update_db_keyspace=True)
import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});

// Get an admin object
const admin = database.admin({ environment: "hcd" });

// Create a keyspace
(async function () {
  await admin.createKeyspace("KEYSPACE_NAME", { updateDbKeyspace: true });
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;

public class Example {

  public static void main(String[] args) {
    // Get a database
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Get an admin object
    DatabaseAdmin admin = database.getDatabaseAdmin();

    // Create a keyspace
    KeyspaceDefinition definition = new KeyspaceDefinition().name("KEYSPACE_NAME");
    CreateKeyspaceOptions keyspaceOptions = new CreateKeyspaceOptions().updateDBKeyspace(true);
    admin.createKeyspace(definition, keyspaceOptions);
  }
}

This option only applies to the clients.

Create a keyspace and specify replication options

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)

# Get an admin object
admin = database.get_database_admin()

# Create a keyspace
admin.create_keyspace(
    "KEYSPACE_NAME",
    replication_options={"class": "SimpleStrategy", "replication_factor": 3},
)
import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});

// Get an admin object
const admin = database.admin({ environment: "hcd" });

// Create a keyspace
(async function () {
  await admin.createKeyspace("KEYSPACE_NAME", {
    replication: {
      class: "SimpleStrategy",
      replicationFactor: 3,
    },
  });
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;

public class Example {

  public static void main(String[] args) {
    // Get a database
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Get an admin object
    DatabaseAdmin admin = database.getDatabaseAdmin();

    // Create a keyspace
    KeyspaceDefinition definition =
        new KeyspaceDefinition().name("KEYSPACE_NAME").simpleStrategy(3);
    admin.createKeyspace(definition);
  }
}
curl -sS -L -X POST "API_ENDPOINT/v1" \
  --header "Token: APPLICATION_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "createKeyspace": {
    "name": "KEYSPACE_NAME",
    "options": {
      "replication": {
        "class": "SimpleStrategy", "replication_factor": 3
      }
    }
  }
}'

Client reference

  • Python

  • TypeScript

  • Java

  • curl

For more information, see the client reference.

For more information, see the client reference.

For more information, see the client reference.

Client reference documentation is not applicable for HTTP.

Was this helpful?

Give Feedback

How can we improve the documentation?

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