> ## 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 an `EncryptedIndex` instance based on the provided configuration.

```typescript theme={null}
async createIndex(
    indexName: string, 
    indexKey: Uint8Array, 
    indexConfig: IndexIVFPQModel | IndexIVFFlatModel | IndexIVFModel,
    embeddingModel?: string
): Promise<EncryptedIndex>
```

### Parameters

| Parameter        | Type                                                                                                                                            | Description                                                                                                                                                 |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `indexName`      | `string`                                                                                                                                        | Name of the index to create. Must be unique within the CyborgDB service.                                                                                    |
| `indexKey`       | `Uint8Array`                                                                                                                                    | 32-byte encryption key for the index, used to secure the index data.                                                                                        |
| `indexConfig`    | [`IndexIVFPQModel`](../types#indexivfpqmodel) \| [`IndexIVFFlatModel`](../types#indexivfflatmodel) \| [`IndexIVFModel`](../types#indexivfmodel) | Configuration object specifying the index type (`ivf`, `ivfpq`, or `ivfflat`) and relevant parameters such as `dimension`, `nLists`, `pqDim`, and `pqBits`. |
| `embeddingModel` | `string`                                                                                                                                        | *(Optional)* Name of the embedding model used to auto-generate embeddings on the server. Defaults to `undefined`, no auto-generation.                       |

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

### Returns

`Promise<EncryptedIndex>`: A Promise that resolves to an instance of the newly created encrypted index.

### Exceptions

<AccordionGroup>
  <Accordion title="Error">
    * Throws if the index name already exists on the server.
    * Throws if the index configuration is invalid or missing required parameters.
    * Throws if the encryption key is not exactly 32 bytes.
    * Throws if the embedding model is not supported by the server.
  </Accordion>

  <Accordion title="Network/API Errors">
    * Throws if the API request fails due to network issues.
    * Throws if the server returns an HTTP error status.
    * Throws if authentication fails (invalid API key).
  </Accordion>
</AccordionGroup>

### Example Usage

#### Basic IVFFlat Index Creation

```typescript theme={null}
import { Client, IndexIVFFlatModel } from 'cyborgdb';

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

const indexName = "my_vector_index";
const indexKey = crypto.getRandomValues(new Uint8Array(32)); // Generate secure 32-byte key

const indexConfig: IndexIVFFlatModel = {
    dimension: 768,
    nLists: 1024,
    metric: 'cosine'
};

try {
    const index = await client.createIndex(
        indexName, 
        indexKey, 
        indexConfig
    );
    console.log('Index created successfully:', index);
} catch (error) {
    console.error('Failed to create index:', error.message);
}
```

#### IVFPQ Index with Embedding Model

```typescript theme={null}
import { Client, IndexIVFPQModel } from 'cyborgdb';

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

const indexName = "semantic_search_index";
const indexKey = crypto.getRandomValues(new Uint8Array(32));

const indexConfig: IndexIVFPQModel = {
    dimension: 1536,
    nLists: 2048,
    metric: 'cosine',
    pqDim: 64,
    pqBits: 8
};

try {
    const index = await client.createIndex(
        indexName,
        indexKey,
        indexConfig,
        'all-MiniLM-L6-v2'  // OpenAI embedding model
    );
    
    console.log('IVFPQ index with embeddings created successfully');
} catch (error) {
    console.error('Index creation failed:', error.message);
}
```
