Initiate resetting a user's password
Initiate a user's password reset from your integration with POST /forgot.
Audience: Developers integrating password resets into an application.
Provide the existing user's email address and the password reset URL in the required email and url query parameters. The endpoint creates a reset URL with a merchant-generated reset token (a temporary value generated by the business using Stax to identify the password reset request), then stores the request in the database.
Use this endpoint with GET /reset/{token} and POST /reset/{token}. These are route templates, not copy-paste request URLs: {token} names the path parameter that you replace with the actual token from the generated reset URL. The braces are not part of the token. Use the generated reset URL as the source of the token, not a Dashboard ID. This request does not require a token or a merchant ID as input.
Send a password reset request
- Prepare your merchant API key, an existing user's email address, and your application's password reset URL. The API uses your URL to construct the reset link.
- Save the following code as
initiate_password_reset.py. It uses Python 3's standard library and prompts for your values instead of embedding credentials or dummy IDs. - Run
python3 initiate_password_reset.pyand enter the requested values. The API key input is hidden.
import getpass
import json
import sys
import urllib.error
import urllib.parse
import urllib.request
def main():
api_key = getpass.getpass("Merchant API key: ").strip()
email = input("Existing user's email address: ").strip()
reset_url = input("Your application's password reset URL: ").strip()
if not all((api_key, email, reset_url)):
print("Enter an API key, email address, and password reset URL.", file=sys.stderr)
return 1
# Encode query values so characters in the email and reset URL stay intact.
query = urllib.parse.urlencode({"email": email, "url": reset_url})
request = urllib.request.Request(
"https://apiprod.fattlabs.com/forgot?" + query,
headers={
"Authorization": "Bearer " + api_key,
"Accept": "application/json",
},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
status = response.status
body = response.read().decode("utf-8", errors="replace")
except urllib.error.HTTPError as error:
print(f"HTTP {error.code}", file=sys.stderr)
print(error.read().decode("utf-8", errors="replace"), file=sys.stderr)
return 1
except (urllib.error.URLError, TimeoutError, OSError) as error:
print(f"Request failed: {error}", file=sys.stderr)
return 1
try:
result = json.loads(body)
except json.JSONDecodeError:
print(f"Unexpected response (HTTP {status}): {body!r}", file=sys.stderr)
return 1
if status != 200 or result != ["email sent"]:
print(f"Unexpected response (HTTP {status}): {result!r}", file=sys.stderr)
return 1
print(f"HTTP {status}")
print(json.dumps(result, indent=2))
return 0
if __name__ == "__main__":
sys.exit(main())Expected output:
HTTP 200
[
"email sent"
]Check the result
On success, the endpoint returns HTTP 200 with this response:
[
"email sent"
]If the email address is not recognized, the web page returns "e-mail not recognized". Confirm that you supplied an existing user's email address before retrying.
For HTTP 422, check the returned validation errors. The documented response can include url and email arrays containing messages such as "The url field is required." and "The selected email is required." Supply both query parameters and verify the email address. For a network failure, check your connection before retrying; for an unexpected response, inspect the status and response body printed by the example.
The generated token expires after 14 days. If the user does not complete the password reset within this window, initiate a new reset request.
