---
title: "exportKey | Grafana k6 documentation"
description: "exportKey exports a key in an external, portable format."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# exportKey

The `exportKey()` method takes a [CryptoKey](/docs/k6/latest/javascript-api/crypto/cryptokey/) object as input and exports it in an external, portable format.

Note that for a key to be exportable, it must have been created with the `extractable` flag set to `true`.

## Usage

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
exportKey(format, key)
```

## Parameters

Expand table

| Name     | Type                                                          | Description                                                                                                                                                                                          |
|----------|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `format` | `string`                                                      | Defines the data format in which the key should be exported. Depending on the algorithm and key type, the data format could vary. Currently supported formats are `raw`, `jwk`, `spki`, and `pkcs8`. |
| `key`    | [CryptoKey](/docs/k6/latest/javascript-api/crypto/cryptokey/) | The [key](/docs/k6/latest/javascript-api/crypto/cryptokey/) to export.                                                                                                                               |

### Supported algorithms

Expand table

| AES-CBC                                                               | AES-CTR                                                               | AES-GCM                                                               | AES-KW | ECDH                                                                                | ECDSA                                                               | HMAC                                                                          | PBKDF2                                                                | RSA-OAEP                                                                                | RSASSA-PKCS1-v1\_5                                                                      | RSA-PSS                                                                                 |
|-----------------------------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------------------------------------------|--------|-------------------------------------------------------------------------------------|---------------------------------------------------------------------|-------------------------------------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| ✅ [AesCbcParams](/docs/k6/latest/javascript-api/crypto/aescbcparams/) | ✅ [AesCtrParams](/docs/k6/latest/javascript-api/crypto/aesctrparams/) | ✅ [AesGcmParams](/docs/k6/latest/javascript-api/crypto/aesgcmparams/) | ❌      | ✅ [EcdhKeyDeriveParams](/docs/k6/latest/javascript-api/crypto/ecdhkeyderiveparams/) | ✅ [EcdsaParams](/docs/k6/latest/javascript-api/crypto/ecdsaparams/) | ✅ [HmacKeyGenParams](/docs/k6/latest/javascript-api/crypto/hmackeygenparams/) | ✅ [Pbkdf2Params](/docs/k6/latest/javascript-api/crypto/pbkdf2params/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) |

### Supported formats

- `ECDH` and `ECDSA` algorithms have support for `pkcs8`, `spki`, `raw` and `jwk` formats.
- `RSA-OAEP`, `RSASSA-PKCS1-v1_5` and `RSA-PSS` algorithms have support for `pkcs8`, `spki` and `jwk` formats.
- `AES-*` and `HMAC` algorithms have currently support for `raw` and `jwk` formats.
- `PBKDF2` algorithm has support for `raw` format only.

## Return Value

A `Promise` that resolves to a new `ArrayBuffer` or an [JsonWebKey](/docs/k6/latest/javascript-api/crypto/jsonwebkey/) object/dictionary containing the key.

## Throws

Expand table

| Type                 | Description                                         |
|----------------------|-----------------------------------------------------|
| `InvalidAccessError` | Raised when trying to export a non-extractable key. |
| `NotSupportedError`  | Raised when trying to export in an unknown format.  |
| `TypeError`          | Raised when trying to use an invalid format.        |

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
export default async function () {
  /**
   * Generate a symmetric key using the AES-CBC algorithm.
   */
  const generatedKey = await crypto.subtle.generateKey(
    {
      name: 'AES-CBC',
      length: '256',
    },
    true,
    ['encrypt', 'decrypt']
  );

  /**
   * Export the key in raw format.
   */
  const exportedKey = await crypto.subtle.exportKey('raw', generatedKey);

  /**
   * Reimport the key in raw format to verify its integrity.
   */
  const importedKey = await crypto.subtle.importKey('raw', exportedKey, 'AES-CBC', true, [
    'encrypt',
    'decrypt',
  ]);

  console.log(JSON.stringify(importedKey));
}
```
