> ## 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.

```cpp theme={null}
// Overload 1: With IndexConfig (explicit configuration)
std::unique_ptr<cyborg::EncryptedIndex>
    CreateIndex(const std::string index_name,
                const std::array<uint8_t, 32>& index_key,
                const IndexConfig& index_config,
                const std::optional<DistanceMetric>& metric = std::nullopt,
                cyborg::Logger* logger = nullptr);

// Overload 2: Without IndexConfig (uses IndexIVFSQ 16-bit defaults)
std::unique_ptr<cyborg::EncryptedIndex>
    CreateIndex(const std::string index_name,
                const std::array<uint8_t, 32>& index_key,
                const std::optional<DistanceMetric>& metric = std::nullopt,
                cyborg::Logger* logger = nullptr);
```

### Parameters

| Parameter      | Type                                  | Description                                                                                                        |
| -------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| `index_name`   | `std::string`                         | Name of the index to create (must be unique).                                                                      |
| `index_key`    | `std::array<uint8_t, 32>`             | 32-byte encryption key for the index, used to secure index data.                                                   |
| `index_config` | [`IndexConfig`](../types#indexconfig) | *(Optional in overload 2)* Configuration for the index type. When not provided, uses IndexIVFSQ (16-bit) defaults. |
| `metric`       | `std::optional<DistanceMetric>`       | *(Optional)* Distance metric to override the one set in index\_config (default is `std::nullopt`).                 |
| `logger`       | [`cyborg::Logger*`](./logger)         | *(Optional)* Pointer to a logger instance for capturing operation logs (default is `nullptr`).                     |

### Returns

`std::unique_ptr<cyborg::EncryptedIndex>`: A pointer to the newly created index ([`EncryptedIndex`](../encrypted-index)).

### Exceptions

<AccordionGroup>
  <Accordion title="std::invalid_argument">
    * Throws if the index name is not unique.
    * Throws if the index configuration is invalid.
  </Accordion>

  <Accordion title="std::runtime_error">
    * Throws if the index could not be created.
  </Accordion>
</AccordionGroup>

### Example Usage

#### Automatic Index Config

```cpp theme={null}
#include "cyborgdb_core/client.hpp"
#include "cyborgdb_core/encrypted_index.hpp"
#include "cyborgdb_core/logger.hpp"
#include <array>

// ... Initialize the client ...

// Create a secure 32-byte key (example: all zeros)
std::array<uint8_t, 32> index_key = {0};

auto index = client.CreateIndex("my_index", index_key);
```

#### Explicit Index Config

```cpp theme={null}
#include "cyborgdb_core/client.hpp"
#include "cyborgdb_core/encrypted_index.hpp"
#include "cyborgdb_core/logger.hpp"
#include <array>

// ... Initialize the client ...

// Create a secure 32-byte key (example: all zeros)
std::array<uint8_t, 32> index_key = {0};

// Example vector dimensionality & number of lists
const size_t vector_dim = 1024;
const size_t num_lists = 128;

// Create an index configuration (e.g., using an IVFFlat configuration)
IndexIVFFlat index_config(vector_dim);

// Optional: Create and configure a logger
cyborg::Logger logger;
logger.Configure(LogLevel::Info, true, "index_creation.log");

auto index = client.CreateIndex("my_index", index_key, index_config, DistanceMetric::Euclidean, &logger);
```
