This endpoint accepts a request made with an API Key in the Authorization header and issues an ephemeral authentication token (expiring in 24 hours) in response.
Ephemeral Authentication Tokens
Request an ephemeral authentication token (a temporary credential valid for 24 hours) to authenticate subsequent API calls without sending your long-term API key each time. The token is a JSON Web Token (JWT), a signed credential that identifies the user making the request.
API Key Requirements
- Generate an API key in your Stax merchant account: open Apps > API keys > Launch > + New Key.
- Keep the API key on your server; do not embed it in browser or mobile code. Treat the temporary token as a sensitive credential too.
- Send the API key in the
Authorizationheader asBearerfollowed by the key.
Request a Token for Your API Key's User
For standard API authentication, start without query parameters. This requests a token for the Stax user associated with your API key and avoids looking up a user ID. For single sign-on (SSO), which lets a user access Stax without entering separate login credentials, specify a user as described below instead.
- Run Python 3 on your server. This example uses only the Python standard library.
- Save the code below as
request_ephemeral_token.py, then runpython3 request_ephemeral_token.py. - Enter your merchant API key from Apps > API keys > Launch at the hidden prompt.
import getpass
import json
import sys
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
def main():
api_key = getpass.getpass("Merchant API key: ").strip()
if not api_key:
raise ValueError("Enter a nonempty merchant API key.")
request = Request(
"https://apiprod.fattlabs.com/ephemeral",
headers={
"Authorization": "Bearer " + api_key,
"Accept": "application/json",
},
method="GET",
)
with urlopen(request, timeout=30) as response:
payload = json.load(response)
token = payload.get("token") if isinstance(payload, dict) else None
if not isinstance(token, str) or not token.strip():
raise ValueError("The response did not contain a nonempty token string.")
# Keep credentials out of terminal output and shared logs.
print("Ephemeral token received (valid for 24 hours).")
# In your server application, pass this value to subsequent API requests.
return token
if __name__ == "__main__":
try:
main()
except HTTPError as error:
if error.code == 422:
message = "Request rejected (HTTP 422). Use a merchant API key, not an ephemeral token."
else:
message = "Token request failed (HTTP " + str(error.code) + ")."
print(message, file=sys.stderr)
sys.exit(1)
except URLError:
print("Could not reach Stax. Check server connectivity and retry.", file=sys.stderr)
sys.exit(1)
except (ValueError, OSError) as error:
print("Token request failed: " + str(error), file=sys.stderr)
sys.exit(1)
except (EOFError, KeyboardInterrupt):
print("Token request canceled.", file=sys.stderr)
sys.exit(1)On success, the script prints:
Ephemeral token received (valid for 24 hours).The script validates that the response contains a nonempty token string without printing the credential. In your server application, use the returned token in subsequent API calls by setting the Authorization header to Bearer followed by that token. The script exits with an error if input is missing, the request fails, or the response is unreadable or lacks a token.
Optional: Compare Authentication Modes
Standard Authentication:
- Omit both
user_idanduser_emailto authenticate as the Stax user associated with your API key. - The endpoint sets that user as the JWT subject (the user identified by the token).
This method is not suitable for SSO capabilities because SSO requires specifying a particular user.
User-Specific Authentication:
- Send either
user_emailoruser_idas a query parameter to request a token for a specific Stax user. Both parameters are optional strings with no default value; omitting both selects the API key's user. - Prefer
user_emailwhen you already know the existing user's email address, so you do not need to look up an ID. URL-encode the email address before including it in the query string. - If you use
user_id, use the Stax user ID returned when you create the merchant's user account. No example ID is required for the standard request above. - Use the resulting token for SSO or API calls on behalf of the specified user.
Authenticate as a specific user for single sign-on.
Allows API calls to be made on behalf of the specified user using their permissions.
Key Benefits
Tokens expire in 24 hours for enhanced security.
Support for both standard and user-specific token generation.
Request a user-specific token for single sign-on.
