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

# Generate Key

Generates a random 32-byte (256-bit) key for creating an encrypted index with an SDK-supplied key.

```go theme={null}
func GenerateKey() ([]byte, error)
```

`GenerateKey` is a package-level function, not a `Client` method. The package also exports the key length as `cyborgdb.KeySize` (`32`).

### Returns

A 32-byte key read from Go's `crypto/rand`. The error, which wraps `ErrKeyGeneration`, is non-nil only if the operating system's random source fails.

Unlike the Python SDK, there is no option to save the key to a file.

<Warning>An index created with an SDK-supplied key can only be opened with that exact key. If the key is lost, the index's data can't be decrypted. Store it in a secrets manager, or use a KMS-backed index. See [Managing Encryption Keys](../../guides/advanced/managing-keys).</Warning>

### Example Usage

```go theme={null}
package main

import (
    "context"
    "encoding/hex"
    "fmt"
    "log"

    "github.com/cyborginc/cyborgdb-go"
)

func main() {
    client, err := cyborgdb.NewClient("http://localhost:8000", "your-api-key")
    if err != nil {
        log.Fatal(err)
    }

    key, err := cyborgdb.GenerateKey()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(len(key))
    // Output: 32

    _, err = client.CreateIndex(context.Background(), &cyborgdb.CreateIndexParams{
        IndexName: "my-secure-index",
        IndexKey:  key,
    })
    if err != nil {
        log.Fatal(err)
    }

    // Keep the key somewhere safe, e.g. as hex in a secrets manager
    keyHex := hex.EncodeToString(key)
    _ = keyHex
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.