> ## 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 IVF index type:

<CodeGroup>
  ```python Python SDK icon="python" theme={null}
  from cyborgdb import Client, IndexIVF, generate_key

  # Create a client
  client = Client('http://localhost:8000', 'your-api-key')

  # Create an IVF index config (can also be IVFFlat/IVFPQ)
  # Using an example vector dimension of 768, and number of lists of 1024
  index_config = IndexIVF(
      type='ivf',
      dimension=768,
      n_lists=1024,
      metric='cosine'
  )

  # Generate an encryption key for the index
  index_key = generate_key()  # Returns 32-byte key as bytes

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

  ```javascript JavaScript SDK icon="js" theme={null}
  import { Client, IndexIVFModel } from 'cyborgdb';

  // Create a client
  const client = new Client('http://localhost:8000', 'your-api-key');

  // Create an IVF index config (can also be IVFFlat/IVFPQ)  
  // Using an example vector dimension of 768, and number of lists of 1024
  const indexConfig = {
      type: 'ivf',
      dimension: 768,
      nLists: 1024,
      metric: 'cosine'
  };

  // Generate an encryption key for the index
  const indexKey = crypto.getRandomValues(new Uint8Array(32));

  // Create an encrypted index
  const index = await client.createIndex("my_index", indexKey, indexConfig);
  ```

  ```typescript TypeScript SDK icon="code" theme={null}
  import { Client, IndexIVFModel } from 'cyborgdb';

  // Create a client
  const client = new Client('http://localhost:8000', 'your-api-key');

  // Create an IVF index config (can also be IVFFlat/IVFPQ)
  // Using an example vector dimension of 768, and number of lists of 1024
  const indexConfig: IndexIVFModel = {
      type: 'ivf',
      dimension: 768,
      nLists: 1024,
      metric: 'cosine'
  };

  // Generate an encryption key for the index
  const indexKey = crypto.getRandomValues(new Uint8Array(32));

  // Create an encrypted index
  const index = await client.createIndex("my_index", indexKey, indexConfig);
  ```

  ```bash cURL icon="rectangle-terminal" theme={null}
  curl -X POST "http://localhost:8000/v1/indexes/create" \
       -H "X-API-Key: your-api-key" \
       -H "Content-Type: application/json" \
       -d '{
         "index_name": "my_index",
         "index_key": "your_64_character_hex_key_here",
         "index_config": {
           "type": "ivf",
           "dimension": 768,
           "n_lists": 1024,
           "metric": "cosine"
         }
       }'
  ```
</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>

## Index Caching

The service-based SDKs handle caching automatically on the server side. Unlike the embedded SDKs, you don't need to specify cache sizes when creating indexes.

The CyborgDB service optimizes query performance through:

* Automatic index caching based on usage patterns
* Server-side query optimization
* Efficient index loading and memory management

## Automatic Embedding Generation

Both the Python and JavaScript/TypeScript service SDKs support automatic embedding generation. You can specify an embedding model when creating the index:

<CodeGroup>
  ```python Python SDK icon="python" theme={null}
  from cyborgdb import Client, IndexIVF, generate_key

  # Create a client
  client = Client('http://localhost:8000', 'your-api-key')

  # Example index config with dimensions matching the embedding model
  index_config = IndexIVF(
      type='ivf',
      dimension=384,  # dimension for all-MiniLM-L6-v2
      n_lists=1024,
      metric='cosine'
  )

  # Generate an encryption key for the index
  index_key = generate_key()

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

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

  ```javascript JavaScript SDK icon="js"   theme={null}
  import { Client, IndexIVFModel } from 'cyborgdb';

  // Create a client
  const client = new Client('http://localhost:8000', 'your-api-key');

  // Example index config with dimensions matching the embedding model
  const indexConfig = {
      type: 'ivf',
      dimension: 384,  // dimension for all-MiniLM-L6-v2
      nLists: 1024,
      metric: 'cosine'
  };

  // Generate an encryption key for the index
  const indexKey = crypto.getRandomValues(new Uint8Array(32));

  // Set embedding model (from HuggingFace)
  const embeddingModel = "sentence-transformers/all-MiniLM-L6-v2";

  // Create an encrypted index with managed embedding generation
  const index = await client.createIndex("my_index", indexKey, indexConfig, embeddingModel);
  ```

  ```typescript TypeScript SDK icon="code" theme={null}
  import { Client, IndexIVFModel } from 'cyborgdb';

  // Create a client
  const client = new Client('http://localhost:8000', 'your-api-key');

  // Example index config with dimensions matching the embedding model
  const indexConfig: IndexIVFModel = {
      type: 'ivf', 
      dimension: 384,  // dimension for all-MiniLM-L6-v2
      nLists: 1024,
      metric: 'cosine'
  };

  // Generate an encryption key for the index
  const indexKey = crypto.getRandomValues(new Uint8Array(32));

  // Set embedding model (from HuggingFace)
  const embeddingModel = "sentence-transformers/all-MiniLM-L6-v2";

  // Create an encrypted index with managed embedding generation
  const index = await client.createIndex("my_index", indexKey, indexConfig, embeddingModel);
  ```

  ```bash cURL icon="rectangle-terminal" theme={null}
  curl -X POST "http://localhost:8000/v1/indexes/create" \
       -H "X-API-Key: your-api-key" \
       -H "Content-Type: application/json" \
       -d '{
         "index_name": "my_index",
         "index_key": "your_64_character_hex_key_here",
         "index_config": {
           "type": "ivf",
           "dimension": 384,
           "n_lists": 1024,
           "metric": "cosine"
         },
         "embedding_model": "sentence-transformers/all-MiniLM-L6-v2"
       }'
  ```
</CodeGroup>

## API Reference

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

<CardGroup cols={3}>
  <Card title="REST API Reference" href="../../rest-api/client/create-index" icon="rectangle-terminal">
    REST API reference for `/v1/indexes/create`
  </Card>

  <Card title="Python SDK Reference" href="../../python-sdk/client/create-index" icon="python">
    API reference for `create_index()` in Python
  </Card>

  <Card title="JS/TS SDK Reference" href="../../js-ts-sdk/client/create-index" icon="js">
    API reference for `create_index()` in JavaScript/TypeScript
  </Card>
</CardGroup>
