---
schema_version: "1"
id: bb7d444ba6a391d4bc2251d12bd3140da4724bc3df10ced8a19f811c0f5be730
title: Headless Forgot Password JavaScript Examples
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/security/headless-identity-impl-guide/guides/headless-identity/headless-identity-javascript-forgotpassword.md
canonical_url: https://developer.salesforce.com/docs/platform/headless-identity-impl-guide/guide/headless-identity-javascript-forgotpassword.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: fdb7f32759ae0f72585baa5b98a03df40ef54b7693bc8160585129954b1764c5
products:
  - Headless Identity Implementation Guide
  - Security
---

# Headless Forgot Password JavaScript Examples

Use these high-level examples to understand how to implement the Headless Forgot Password Flow for a single-page app.

The Headless Forgot Password Flow contains two requests to the same endpoint. You initialize the flow with a request to Headless Forgot Password API. Here’s a function that you can call to initialize the password reset.

```nolang
//This is the function call to initialize the forgot password request
forgotPasswordRequest(username, null, null, recapchaToken, forgotPasswordProcess.init, callbackFunction);
```

This request results in Salesforce sending the user a one-time password (OTP).

For the second request, you pass the username, new password, and OTP to Headless Forgot Password API. Here’s a function that you can call to complete the password change.

```nolang
//This is the function call to complete the forgot password request
forgotPasswordRequest(username, password, otp, null, forgotPasswordProcess.changePassword, callbackFunction)
```

Because both requests call the same endpoint, you can use one function for both calls.

```nolang
function forgotPasswordRequest(username, password, otp, recapchaToken, forgotPasswordProcessStep, callbackFunction) {
    client = new XMLHttpRequest();
    client.open("POST", expDomain + forgotPasswordURI, true);
    client.setRequestHeader("Content-Type", "application/json");

    requestBody = {
    username: username,
    newpassword: password,
    otp: otp,
    recaptcha: recapchaToken
    }

    client.send(JSON.stringify(requestBody)); 

    client.onreadystatechange = function() {
      if(this.readyState == 4) {
        if (this.status == 200) {
          callbackFunction(JSON.parse(client.response), username, forgotPasswordProcessStep)
        } else {
          this.onError("An Error Occured during Forgot Password Step: " + forgotPasswordProcessStep, client.response); 
        }
      } 
    }
  }
```

