> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cyborg.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Index

Creates and returns a new encrypted index based on the provided configuration.

```python theme={null}
def create_index(self,
                 index_name: str,
                 index_key: bytes,
                 index_config: IndexConfig = None,
                 embedding_model: str = None,
                 metric: str = None,
                 logger: Logger = None)
```

### Parameters

| Parameter         | Type                                  | Description                                                                                                                                           |
| ----------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `index_name`      | `str`                                 | Name of the index to create. Must be unique.                                                                                                          |
| `index_key`       | `bytes`                               | 32-byte encryption key for the index, used to secure the index data.                                                                                  |
| `index_config`    | [`IndexConfig`](../types#indexconfig) | *(Optional)* Configuration specifying the index type and parameters. When not provided, uses `IndexIVFSQ(sq_bits=16)` defaults.                       |
| `embedding_model` | `str`                                 | *(Optional)* Name of the SentenceTransformer model to use for automatic text embeddings.                                                              |
| `metric`          | `str`                                 | *(Optional)* Distance metric to use: "euclidean", "cosine", or "squared\_euclidean". Overrides the metric in `index_config`. Defaults to "euclidean". |
| `logger`          | [`Logger`](./logger)                  | *(Optional)* Logger instance for capturing operation logs. Defaults to `None`.                                                                        |

<Note>For more info on auto-generating embeddings, refer to [Auto-Generate Embeddings](../../guides/data-operations/add-items#automatic-embedding-generation).</Note>

### Returns

`EncryptedIndex`: An instance of the newly created encrypted index.

### Exceptions

<AccordionGroup>
  <Accordion title="ValueError">
    * Throws if the index name is not unique.
    * Throws if the index configuration is invalid.
    * Throws if the index could not be created.
  </Accordion>
</AccordionGroup>

### Example Usage

#### Automatic Setup

```python theme={null}
import cyborgdb_core as cyborgdb
import secrets

index_location = cyborgdb.DBConfig(location='redis', connection_string="redis://localhost")
config_location = cyborgdb.DBConfig(location='redis', connection_string="redis://localhost")
items_location = cyborgdb.DBConfig(location='postgres', table_name="items", connection_string="host=localhost dbname=postgres")

client = cyborgdb.Client(
    api_key=api_key,
    index_location=index_location, 
    config_location=config_location, 
    items_location=items_location
)

index_name = "my_index"
index_key = secrets.token_bytes(32)  # Generate a secure 32-byte encryption key

# Create with automatic IndexIVFSQ defaults (16-bit scalar quantization)
index = client.create_index(
    index_name=index_name,
    index_key=index_key
)
```

#### Explicit Setup

```python theme={null}
import cyborgdb_core as cyborgdb
import secrets

index_location = cyborgdb.DBConfig(location='redis', connection_string="redis://localhost")
config_location = cyborgdb.DBConfig(location='redis', connection_string="redis://localhost")
items_location = cyborgdb.DBConfig(location='postgres', table_name="items", connection_string="host=localhost dbname=postgres")

client = cyborgdb.Client(
    api_key=api_key, 
    index_location=index_location, 
    config_location=config_location, 
    items_location=items_location
)

index_name = "my_index"
index_key = secrets.token_bytes(32)  # Generate a secure 32-byte encryption key
index_config = cyborgdb.IndexIVF(
    dimension=128  # n_lists will be set automatically during training
)

# Optional: Set up logging
logger = cyborgdb.Logger.instance()
logger.configure(level="info", to_file=True, file_path="index_creation.log")

index = client.create_index(
    index_name=index_name,
    index_key=index_key,
    index_config=index_config,
    metric="cosine",  # Override default metric
    logger=logger
)
```
