> ## 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,                                      // required
    indexKey: Uint8Array,                                   // required
    indexConfig?: IndexConfig,                              // optional, default: IndexIVFFlat()
    metric?: 'euclidean' | 'squared_euclidean' | 'cosine',  // optional, default: 'euclidean'
    embeddingModel?: string                                 // optional, default: undefined
}): 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`    | [`IndexIVFPQ`](../types#indexivfpq) \| [`IndexIVFFlat`](../types#indexivfflat) \| [`IndexIVFSQ`](../types#indexivfsq) | *(Optional)* Configuration object specifying the index type (`ivfflat`, `ivfpq`, or `ivfsq`). Defaults to `{ type: 'ivfflat', dimension: undefined }`. |
| `metric`         | `'euclidean' \| 'squared_euclidean' \| 'cosine'`                                                                      | *(Optional)* Distance metric to use for the index. Defaults to `'euclidean'`.                                                                          |
| `embeddingModel` | `string`                                                                                                              | *(Optional)* Name of the embedding model used to auto-generate embeddings on the server. Defaults to `undefined`, no auto-generation.                  |

### 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="Service Errors">
    * Throws if the CyborgDB service is unavailable or unreachable.
    * Throws if there are internal server errors on the CyborgDB service.
  </Accordion>
</AccordionGroup>

### Example Usage

#### Automatic Index Config

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

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

const indexName = "my_vector_index";
const indexKey: Uint8Array = client.generateKey(); // Generate secure 32-byte key

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

<Note>For more info on `generateKey`, refer to [Generate Key](./generate-key).</Note>

#### IVFFlat Index with Embedding Model

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

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

const indexName = "semantic_search_index";
const indexKey: Uint8Array = client.generateKey();

const indexConfig: IndexIVFFlat = {
    type: 'ivfflat',
    dimension: 384
};

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

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