Hosted Backend API
On this page
1.45.0
The hosted backend APIs provide a pre-built solution to get your app up and running using the OAuth2 Authorization Code grant with PKCE. Other documentation shows you how to create these endpoints yourself but this solution allows you to secure your app without writing any backend code. You need only FusionAuth.
Prerequisites#
Be sure to review the Applications section of the FusionAuth user guide to ensure proper configuration before using the hosted endpoints.
The hosted backend endpoints set the following cookies, which are Secure and have a SameSite value of Lax. The cookie settings follow our expert advice on client-side storage.
When you make requests against these endpoints from your JavaScript application, ensure that you send cookies as well. The exact syntax varies, but for many frameworks, you must set withCredentials to true when you make the request.
Cookies Set By the Hosted Backend
| Name | HttpOnly | Description |
|---|---|---|
| app.at | true | The access token for the configured application. This is a JWT and can be presented to your APIs to access data and functionality. |
| app.rt | true | The refresh token for the configured application. Only present if the offline_access scope is requested. This can be presented to FusionAuth to retrieve a new access token. |
| app.idt | false | The Id token for the user for the configured application. Only present if the openid scope is requested. This is a JWT and can be accessed by JavaScript to display user account information. |
| app.at_exp | false | The UNIX epoch timestamp indicating when the access token expires. This can be checked by JavaScript to determine when a refresh token should be used to get a new access token. |
FusionAuth sets the domain on these cookies to .example.com. Here, example is the domain name. FusionAuth can be hosted on that domain or on one of its subdomains. com is the top-level domain. The leading . allows the cookie to match the domain and all its subdomains. If the host is a simple host name or IP address, FusionAuth sets the domain to that (for example, localhost or 127.0.0.1). If FusionAuth is on a nested domain, then it sets cookies on the broadest domain that is not a top-level domain.
FusionAuth and the application must share a domain or use sibling subdomains for these endpoints to share cookies.
When developing against a FusionAuth Cloud instance with a hostname ending in fusionauth.io, requests from applications outside the fusionauth.io domain fail with a 403 status code.
These endpoints do not work correctly for cross-site requests. A cross-site request comes from an application on a different registrable domain than FusionAuth. For example, requests from app.acme.com to acme.fusionauth.io are cross-site.
If possible, have FusionAuth and your application served by the same domain, using a proxy if needed. For example, serve your app from app.acme.com and FusionAuth from auth.acme.com.
If this configuration is not possible, use one of these alternative methods:
- Develop using a local FusionAuth instance, so both your webapp and FusionAuth are running on
localhost. - Do not use the FusionAuth hosted backend. Write your own backend with a cross-origin cookie policy. See the Express SDK example.
- Configure a custom domain name for the FusionAuth Cloud instance.
Modifying FusionAuth CORS configuration options does not fix this issue because the cookies that FusionAuth writes will not be accessible cross domain.
For example, if your app is on app.example.com and FusionAuth is on auth.example.com the cookies are usable by your application. If FusionAuth is on auth.department.division.example.com and the app lives on app.otherdepartment.otherdivision.example.com, the cookies are still usable because FusionAuth sets them on the example.com domain.
The hosted backend uses the same cookie names for every Application. When two Applications on the same domain use the hosted backend, a login to one Application overwrites the other Application's cookies. A logout from one Application deletes the cookies for both. For example, apps on app1.example.com and app2.example.com share cookies on example.com, even with different client Ids.
To keep the sessions apart, use one of these options:
- Serve each Application, and the FusionAuth hostname that Application uses, from a separate domain, such as
app1.comandapp2.com. - Write your own backend that sets distinct cookie names. Then point the web SDK at that backend, and set
accessTokenExpireCookieNameto the new name of the expiration cookie. See Web SDK Configuration. - Use DPoP mode in the web SDK. DPoP mode doesn't use the hosted backend, and stores tokens separately for each client Id.
Login#
This API starts an OAuth2 Authorization Code grant by building a valid request and then redirecting the browser to our /oauth2/authorize endpoint. If the user is not logged in the user is presented with the login page and prompted for credentials before being redirected back to the Callback endpoint.
To use this API, redirect the browser to this route. This is not meant to be called by non-browser clients.
Request#
Redirect the browser to this route to start the login flow for the given client Id.
Path Parameters#
client_idThe client Id for your Application.
Query Parameters#
redirect_uriThe URL encoded URL that the browser is redirected to at the end of the login flow. If provided, this URL must be included in the Authorized Redirect URLs array for your application. If not provided, the default is the first value in the Authorized Redirect URLs array configured for your application. This parameter is validated the same as if it were passed to /oauth2/authorize, however when using this endpoint FusionAuth passes Callback as the redirect_uri to /oauth2/authorize as that route handles the token exchange.
stateThe value of this parameter is echoed back in the state parameter of the redirect URLThe URL where the authorization server sends the browser after login, with an authorization code or an error. at the end of the login flow.
scopeThe OAuth2 scope parameter to be passed to the /oauth2/authorize endpoint. The format is a URL encoded, space-separated list of scopes (for example, openid+offline_access or openid%20offline_access).
Available scopes:
openid- This scope is used to request theapp.idtId token cookie be returned in the responseoffline_access- This scope is used to request theapp.rtrefresh token cookie be returned in the response
Example Request URL
https://auth.example.com/app/login/297ca84b-69a9-4508-8649-97644e1d0b3d?redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&state=yourStateData&scope=offline_access
Response#
Successful invocations of this route return a 302 redirect to /oauth2/authorize. Other status codes indicate an error.
| Code | Description |
|---|---|
| 200 | After the successful 302 redirect, |
| 302 | A successful request redirects the user to |
| 403 | A forbidden response typically means that the Origin of this request did not pass the FusionAuth CORS filter. Add your app origin to your CORS Configuration as an Allowed Origin. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Register#
This API starts a registration flow by building a valid request and then redirecting the browser to the Register endpoint. This endpoint is nearly identical to the Login endpoint; however the end result is user registration instead of a login. If the user is not logged in the user is presented with the registration page and prompted for credentials before being redirected back to the Callback endpoint. If the user is logged in they are redirected to /oauth2/authorize and subsequently to the Callback endpoint.
Self-service Registration needs to be enabled otherwise this endpoint redirects to Login.
To use this API, redirect the browser to this route. This is not meant to be called by non-browser clients.
Request#
Redirect the browser to this route to start the registration flow for the given client Id.
Path Parameters#
client_idThe client Id for your Application.
Query Parameters#
redirect_uriThe URL encoded URL that the browser is redirected to at the end of the registration. If provided, this URL must be included in the Authorized Redirect URLs array for your application. If not provided, the default is the first value in the Authorized Redirect URLs array configured for your application. This parameter is validated the same as if it were being passed to /oauth2/register, however when using this endpoint FusionAuth passes Callback as the redirect_uri to /oauth2/register as that route handles the token exchange.
stateThe value of this parameter is echoed back in the state parameter of the redirect URL at the end of the registration flow.
scopeThe OAuth2 scope parameter to be passed to the /oauth2/register endpoint. The format is a URL encoded, space-separated list of scopes (for example, openid+offline_access or openid%20offline_access).
Available scopes:
openid- This scope is used to request theapp.idtId token cookie be returned in the responseoffline_access- This scope is used to request theapp.rtrefresh token cookie be returned in the response
Example Request URL
https://auth.example.com/app/register/297ca84b-69a9-4508-8649-97644e1d0b3d?redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&state=yourStateData&scope=offline_access
Response#
Successful invocations of this route return a 302 redirect to /oauth2/register. Other status codes indicate an error.
| Code | Description |
|---|---|
| 200 | After the successful 302 redirect, |
| 302 | A successful request redirects the user to |
| 403 | A forbidden response typically means that the Origin of this request did not pass the FusionAuth CORS filter. Add your app origin to your CORS Configuration as an Allowed Origin. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Callback#
You do not need to call this endpoint directly. The browser is redirected here automatically after a successful login or registration. This is the route that handles the token exchange and sets the cookies on the browser and redirects the browser to the defined redirect_uri.
This endpoint is documented for educational purposes only.
Request#
Query Parameters#
client_idThe client Id for your Application.
tenantIdThe Id of the Tenant that is associated with the Application.
codeThe authorization code.
issThe authorization server's issuer identifier as defined by RFC 9207. This value is the tenant's configured issuer and should be validated by the client against the authorization server metadata to prevent mix-up attacks.
See Issuer Validation for implementation guidance.
Available since1.69.0localeThe locale that was to render the login page, or a previously selected locale by the user during a previous session. This may be useful to know in your own application so you can continue to localize the interface for the language the user selected while on the FusionAuth login page. See the Theme Localization documentation for more information.
stateThe state that was provided on the Authorization request. This parameter will be omitted if the state request parameter was not provided.
userStateThe FusionAuth user state. This value serves as a hint for the user's registration and verification status suitable for message customization and similar uses. userState should not be used to make any security decisions for the user.
The possible values are:
Authenticated- The user is successfully authenticated for the requested application.AuthenticatedNotRegistered- The user is successfully authenticated, but not registered for the requested application.AuthenticatedNotVerified- The user is successfully authenticated, but the user's email address and/or phone number have not been verified.AuthenticatedRegistrationNotVerified- The user is successfully authenticated and registered, but the registration has not been verified for the requested application.
For a CIMD client, the value is always AuthenticatedNotRegistered, because FusionAuth does not register users for CIMD clients.
Example Request URL
https://auth.example.com/app/callback?code=wJfjafZLvo_KH5-D4r-3YwMmStN3yHoZDGmBivjioz0&iss=https%3A%2F%2Fauth.example.com&locale=en&state=eyJjIjoiODVhMDM4NjctZGNjZi00ODgyLWFkZGUtMWE3&userState=Authenticated&client_id=297ca84b-69a9-4508-8649-97644e1d0b3d&tenantId=e707be45-afa8-4881-9efb-4be7288395d2
Response#
After a successful token exchange, the callback sets cookies and returns a 302 redirect to the redirect_uri specified in the initial request. If the authorization code exchange fails, the callback redirects to the same URI with OAuth error parameters. Other status codes indicate an error.
| Code | Description |
|---|---|
| 302 | A successful request redirects the user to |
| 400 | The callback request is invalid, such as when required parameters are missing or the client Id is invalid. The response includes JSON error details. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Response Cookies#
app.at The encoded access token. This cookie is written in the response as a Secure HttpOnly persistent cookie.
app.rt The refresh token. This cookie is written in the response as a Secure HttpOnly persistent cookie. The cookie expiration is configured in the JWT
configuration for the application or the global JWT configuration.
app.idt The Id token for the user for the configured application. Only present if the openid scope is requested. This is a JWT and is a Secure persistent cookie that can be accessed by JavaScript to display user account information.
app.at_exp The unix epoch timestamp indicating when the access token will expire. This is a Secure persistent cookie that can be checked by JavaScript to determine when a refresh token should be used to get a new access token.
Refresh#
This endpoint extracts the app.rt cookie if present and uses it to make a refresh_token request from /oauth2/token. The configuration rules for your Application configuration apply; ensure that Refresh token grant is enabled. If successful a new set of cookies is set on the response that continues to allow access to the application. You can call this any time or you can review the value of app.at_exp and call it when the access token is about to expire.
This API request is made from the client application. The browser must NOT be redirected to this endpoint.
Request#
Call this endpoint with the client Id to refresh the access token using the app.rt cookie.
Path Parameters#
client_idThe client Id for your Application.
Example Request URL
https://auth.example.com/app/refresh/297ca84b-69a9-4508-8649-97644e1d0b3d
Response#
A successful response sets cookies and returns a 200.
| Code | Description |
|---|---|
| 200 | The request was successful. The response sets updated cookies. |
| 400 | The request was not successful. The client needs to reauthorize. Redirect the browser to the |
| 403 | A forbidden response typically means that the Origin of this request did not pass the FusionAuth CORS filter. Add your app origin to your CORS Configuration as an Allowed Origin. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Response Cookies#
app.at The encoded access token. This cookie is written in the response as a Secure HttpOnly persistent cookie.
app.rt The refresh token. This cookie is written in the response as a Secure HttpOnly persistent cookie. The cookie expiration is configured in the JWT
configuration for the application or the global JWT configuration.
app.idt The Id token for the user for the configured application. Only present if the openid scope is requested. This is a JWT and is a Secure persistent cookie that can be accessed by JavaScript to display user account information.
app.at_exp The unix epoch timestamp indicating when the access token will expire. This is a Secure persistent cookie that can be checked by JavaScript to determine when a refresh token should be used to get a new access token.
Me#
This API is used to retrieve information about the currently logged in user. This call takes the app.at cookie value and uses it to call the /oauth2/userinfo API.
This is an API request made from the client application and the browser must NOT be redirected to this endpoint.
Request#
Call this endpoint to retrieve information about the currently logged in user using the app.at cookie.
Example Request URL
https://auth.example.com/app/me
Response#
A successful response returns a 200 with a JSON body containing the user's information. Other status codes indicate an error.
| Code | Description |
|---|---|
| 200 | The request was successful. The response contains a JSON body. |
| 401 | The user is not authorized. Call |
| 403 | A forbidden response typically means that the Origin of this request did not pass the FusionAuth CORS filter. Add your app origin to your CORS Configuration as an Allowed Origin. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Response Body#
applicationIdThe unique Id of the Application for which the user has been authenticated.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is not populated.
birthdateThe birthDate of the User if available.
Format will be in YYYY-MM-DD as defined by the OpenID Connect core specification.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
emailThe email address of the User.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the email scope.
email_verifiedThe verification status of the email. This value is true when the email is verified.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the email scope.
family_nameThe last name of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
given_nameThe first name of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
nameThe full name of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
middle_nameThe middle name of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
phone_numberThe phone number of the user if available. user.phoneNumber or user.mobilePhone will be used in this order of precedence.
When the Scope handling policy is Strict, this field is only populated when the provided token contains the phone scope.
phone_number_verifiedThe verification status of the phone_number. This value is true when the phone number is verified.
This field is only populated when the Scope handling policy is Strict, and the scope claim contains the phone scope.
pictureA URL to a picture of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
preferred_usernameThe username of the user if available.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is only populated when the provided token contains the profile scope.
rolesThe roles assigned to the user in the authenticated Application.
In version 1.50.0 and later, when the Scope handling policy is Strict, this field is not populated.
subThe subject of the token. The value is the unique Id of the FusionAuth user.
{
"applicationId": "3c219e58-ed0e-4b18-ad48-f4f92793ae32",
"birthdate": "1982-03-10",
"email": "richard@piedpiper.com",
"email_verified": true,
"family_name": "Hendricks",
"given_name": "Richard",
"phone_number": "555-555-5555",
"picture": "http://www.piedpiper.com/app/themes/pied-piper/dist/images/photo-richard.png",
"roles": [
"admin"
],
"sub": "858a4b01-62c8-4c2f-bfa7-6d018833bea7"
}
{
"birthdate": "1982-03-10",
"email": "richard@piedpiper.com",
"email_verified": true,
"family_name": "Hendricks",
"given_name": "Richard",
"phone_number": "+15555551212",
"phone_number_verified": true,
"picture": "http://www.piedpiper.com/app/themes/pied-piper/dist/images/photo-richard.png",
"sub": "858a4b01-62c8-4c2f-bfa7-6d018833bea7"
}
Logout#
This API starts a logout. The cookies set on Callback or Refresh are removed. If an SSO session exists, the endpoint ends it.
To use this API, redirect the browser to this route and the router responds with a 302 redirect status code. This is not meant to be called by non-browser clients.
Request#
Redirect the browser to this route to start the logout flow for the given client Id.
Path Parameters#
client_idThe client Id for your Application.
Query Parameters#
post_logout_redirect_uriThe URL encoded URL that the browser is redirected to at the end of the logout flow. This value must be in the Application's Authorized Redirect URLs list. If no post_logout_redirect_uri is provided, the user is redirected to the Logout URL configured for the Application.
Example Request URL
https://auth.example.com/app/logout/297ca84b-69a9-4508-8649-97644e1d0b3d?post_logout_redirect_uri=https%3A%2F%2Fapp.example.com%2F
Response#
Successful invocations of this route return a 302 redirect to /oauth2/logout. Other direct response codes indicate an error. After logout, FusionAuth redirects the browser to the specified post_logout_redirect_uri or the Application's configured Logout URL.
| Code | Description |
|---|---|
| 200 | After the successful 302 redirect, |
| 302 | A successful request redirects the user to |
| 403 | A forbidden response typically means that the Origin of this request did not pass the FusionAuth CORS filter. Add your app origin to your CORS Configuration as an Allowed Origin. |
| 500 | There was a FusionAuth internal error. A stack trace is provided and logged in the FusionAuth log files. |
Related
Learn how the FusionAuth APIs authenticate requests.
API documentation for the FusionAuth Refresh a JWT API.
Client Secret reference for FusionAuth OAuth2 endpoints.
API documentation for the FusionAuth Token OAuth2 endpoint.