importKey method

Constructs a new Key object using the key data specified by the keyData parameter.

Important  As of Microsoft Edge, this method returns a Promise object. See Web Cryptography API updates for more info.



var retVal = SubtleCrypto.importKey(format, keyData, algorithm, extractable, keyUsages);


format [in]

Type: DOMString

A DOM string specifying the format of keyData's key material, such as raw, pkcs8, spki, or jwk. For details, see the below remarks section.

keyData [in]

Type: ArrayBufferView

The key data to import.

algorithm [in]

Type: any

An object identifying the cryptographic algorithm for use with the output Key object (along with any required parameters). If these conflict with the algorithm parameters contained in keyData, the parameters in keyData take precedence.

extractable [in, optional]

Type: bool

Whether or not keying material may be exported from the Key object created by this operation. The default value is false.

keyUsages [in, optional]

Type: sequence<DOMString>

An array containing one or more of the following DOM strings: encrypt, decrypt, sign, verify, derive, wrap, unwrap. These are the operations that the Key object created by this operation will be permitted to perform.

Return value

Type: Promise

A Promise that will return a Key object constructed from the key data specified by the above parameters.

Note  IE11 returns a KeyOperation. See Web Cryptography API updates for more info.


With respect to supported cryptographic algorithms, any key which can be generated can be imported and exported.

The supported formats for both importKey and exportKey are as follows:

  • raw - An unformatted sequence of bytes. Intended for secret keys.
  • pkcs8 - The DER encoding of the PrivateKeyInfo structure from RFC 5208.
  • spki - The DER encoding of the SubjectPublicKeyInfo structure from RFC 5280.
  • jwk - The key is represented as JSON according to the JSON Web Key format.

For a code example, see exportKey.

See also