> ## 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 a New Encrypted Index

To create an encrypted index, you need to specify an index name (must be unique), an index key, and an index configuration. Here's an example with an IVFFlat index type:

<CodeGroup>
  ```python Python theme={null}
  # Import cyborgdb_core or cyborgdb_lite:
  import cyborgdb_core as cyborgdb
  import cyborgdb_lite as cyborgdb
  import secrets

  # Using `memory` storage for this example
  index_location = cyborgdb.DBConfig("memory") 
  config_location = cyborgdb.DBConfig("memory")

  # Create a client
  client = cyborgdb.Client(index_location, config_location)

  # Create an IVFFlat index config (can also be IVF/IVFPQ)
  # Using an example vector dimension of 128, and number of lists of 1024
  index_config = cyborgdb.IndexIVFFlat(dimension=128, n_lists=1024)

  # Generate an encryption key for the index
  index_key = secrets.token_bytes(32)

  # Create an encrypted index
  index = client.create_index("my_index", index_key, index_config)
  ```

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

  // Using `memory` storage for this example
  cyborg::DBConfig index_location(Location::kMemory); 
  cyborg::DBConfig config_location(Location::kMemory);

  // Create a client
  cyborg::Client client(index_location, config_location);

  // Create an IVFFlat index config (can also be IVF/IVFPQ)
  // Using an example vector dimension of 128, and number of lists of 1024
  cyborg::IndexIVFFlat index_config(dimension=128, n_lists=1024);

  // Generate a 32-byte random encryption key
  std::array<uint8_t, 32> index_key;
  std::random_device rd;
  std::generate(index_key.begin(), index_key.end(), [&]() { return rd() % 256; });

  // Create an encrypted index
  cyborg::EncryptedIndex index = client.CreateIndex("my_index", index_key, index_config);
  ```
</CodeGroup>

This creates a new encrypted index with the IVFFlat type. For more details on IVFFlat and other index options, see [Configure an Encrypted Index](./configure-index).

<Warning>The example above creates a random 32 byte (256-bit) index key.
This is fine for evaluation purposes, but for production use, we recommend that you use an HSM or KMS solution.
For more details, see [Managing Encryption Keys](../advanced/managing-keys).</Warning>

## Encrypted Index Caching

For improved query performance, you can enable encrypted index caching by setting a `max_cache_size`:

<CodeGroup>
  ```python Python theme={null}
  # Import cyborgdb_core or cyborgdb_lite:
  import cyborgdb_core as cyborgdb
  import cyborgdb_lite as cyborgdb
  import secrets

  # Using `memory` storage for this example
  index_location = cyborgdb.DBConfig("memory") 
  config_location = cyborgdb.DBConfig("memory")

  # Create a client
  client = cyborgdb.Client(index_location, config_location)

  # Example index config
  index_config = cyborgdb.IndexIVFFlat(dimension=128, n_lists=1024)

  # Generate an encryption key for the index
  index_key = secrets.token_bytes(32)

  # Set max cache size at 1MB
  max_cache_size = 1000000

  # Create an encrypted index
  index = client.create_index("my_index", index_key, index_config, max_cache_size)
  ```

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

  // Using `memory` storage for this example
  cyborg::DBConfig index_location(Location::kMemory); 
  cyborg::DBConfig config_location(Location::kMemory);

  // Create a client
  cyborg::Client client(index_location, config_location);

  // Example index config
  cyborg::IndexIVFFlat index_config(dimension=128, n_lists=1024);

  // Generate a 32-byte random encryption key
  std::array<uint8_t, 32> index_key;
  std::random_device rd;
  std::generate(index_key.begin(), index_key.end(), [&]() { return rd() % 256; });

  // Set max cache size at 1MB
  std::size_t max_cache_size = 1000000;

  // Create an encrypted index
  cyborg::EncryptedIndex index = client.CreateIndex("my_index", index_key, index_config, max_cache_size);
  ```
</CodeGroup>

## Automatic Embedding Generation

<Note>This feature is only available in Python and is experimental as of v0.9.0.</Note>

In the Python version of CyborgDB, you can enable automatic embedding generation for the encrypted index by setting `embedding_model` in `create_index()`:

```python Python theme={null}
# Import cyborgdb_core or cyborgdb_lite:
import cyborgdb_core as cyborgdb
import cyborgdb_lite as cyborgdb

# Using `memory` storage for this example
index_location = cyborgdb.DBConfig("memory")
config_location = cyborgdb.DBConfig("memory")
items_location = cyborgdb.DBConfig("memory")

# Create a client
client = cyborgdb.Client(index_location, config_location, items_location)

# Example index config
index_config = cyborgdb.IndexIVFFlat(dimension=128, n_lists=1024)

# Generate an encryption key for the index
index_key = secrets.token_bytes(32)

# Set embedding model (from HuggingFace)
embedding_model = "all-MiniLM-L6-v2"

# Create an encrypted index with managed embedding generation
index = client.create_index("my_index", index_key, index_config, embedding_model=embedding_model)
```

## API Reference

For more information on creating encrypted indexes, refer to the API reference:

<CardGroup cols={2}>
  <Card title="Python API Reference" href="../../api-reference/python/client/create-index" icon="python">
    API reference for `create_index()` in Python
  </Card>

  <Card title="C++ API Reference" href="../../api-reference/cpp/client/create-index" icon="brackets-curly">
    API reference for `CreateIndex()` in C++
  </Card>
</CardGroup>
