files/en-us/web/api/publickeycredential/parserequestoptionsfromjson_static/index.md
{{APIRef("Web Authentication API")}}{{securecontext_header}}
The parseRequestOptionsFromJSON() static method of the {{domxref("PublicKeyCredential")}} interface converts a {{glossary("JSON type representation")}} into a {{domxref("PublicKeyCredentialRequestOptions")}} instance.
The method is a convenience function for converting information provided by a relying server to a web app in order to request an existing credential.
PublicKeyCredential.parseRequestOptionsFromJSON(options)
options
A {{domxref("PublicKeyCredentialRequestOptions")}} instance.
EncodingError {{domxref("DOMException")}}
options object cannot be converted into a {{domxref("PublicKeyCredentialRequestOptions")}} instance.SecurityError {{domxref("DOMException")}}
The Web Authentication process for authenticating a (registered) user involves a relying party server sending the web app information needed to find an existing credential, including details about the user identity, the relying party, a "challenge", and optionally where to look for the credential: for example on a local built-in authenticator, or on an external one over USB, BLE, and so on.
The web app passes this information to an authenticator to find the credential, by calling navigator.credentials.get() with an argument that contains the server-supplied data as a {{domxref("PublicKeyCredentialRequestOptions")}} instance
The specification does not define how the information needed for requesting a credential is sent.
A convenient approach is for the server to encapsulate the information in a {{glossary("JSON type representation")}} of a {{domxref("PublicKeyCredentialRequestOptions")}} instance, that mirrors its structure but encodes buffer properties such as the challenge as base64url strings.
This object can be serialized to a JSON string, sent to the web app and deserialized, and then converted to a {{domxref("PublicKeyCredentialRequestOptions")}} instance using parseRequestOptionsFromJSON().
When authorizing an already registered user, a relying party server will supply the web app with information about the requested credentials, the relying party, and a challenge.
The code below defines this information in the form described in the options parameter above:
const requestCredentialOptionsJSON = {
challenge: new Uint8Array([139, 66, 181, 87, 7, 203 /* … */]),
rpId: "acme.com",
allowCredentials: [
{
type: "public-key",
id: new Uint8Array([64, 66, 25, 78, 168, 226, 174 /* … */]),
},
],
userVerification: "required",
};
Because this object only uses JSON data types, it can be serialized to JSON using JSON.stringify() and sent to the web app.
JSON.stringify(requestCredentialOptionsJSON);
The web app can deserialize the JSON string back to a requestCredentialOptionsJSON object (not shown).
The parseRequestOptionsFromJSON() method is used to convert that object to the form that can be used in navigator.credentials.get():
// Convert options to form used by get()
const publicKey = PublicKeyCredential.parseRequestOptionsFromJSON(
requestCredentialOptionsJSON, // JSON-type representation
);
navigator.credentials
.get({ publicKey })
.then((returnedCredentialInfo) => {
// Handle the returned credential information here.
})
.catch((err) => {
console.error(err);
});
{{Specifications}}
{{Compat}}