Authenticate HTTP Client Requests

MongoDB Realm enforces non-sync rules and sync rules for all client operations. This means that requests must be made by a logged in user of your Realm app.

To authenticate a GraphQL request as an arbitrary logged in user from any provider, include a valid user access token as a bearer token in the request's Authorization header.

Beaker IconExample

The following request demonstrates how to include an access token for a user with each request. Replace <Access Token> with the access_token value that you want to use.

curl --location --request POST '<yourappid-abcde>/graphql' \
--header 'Authorization: Bearer <Access Token>' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query AllMovies {\n movies {\n title\n year\n }\n}"}'

To get an access token, you need to authenticate with the realm Client HTTP API using the user's login credentials. The Client API authentication endpoints accept valid user credentials in the body of a POST request and use the following URL form:<yourappid-abcde>/auth/providers/<provider type>/login
Beaker IconExample

The following request authenticates a realm user with the client API. The request body specifies the user's login credentials.

curl --location --request POST ''

The authentication request is successful, so the response body includes access_token and refresh_token values for the user. Each of these values is a JSON web token string that identifies the authenticated user and authorizes requests on their behalf.

"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1Nzg5NjY1MTYsImlhdCI6MTU3ODk2NDcxNiwiaXNzIjoiNWUxZDE2ZWM4YWM5M2QzMGFjNDg0ZTk0Iiwic3RpdGNoX2RldklkIjoiMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwIiwic3RpdGNoX2RvbWFpbklkIjoiNWRkODFiYmU3NTFhYzk2ZDM3NmI1Mjg1Iiwic3ViIjoiNWUxM2E0MWUxYjM4ZDM1ODQzMGVkMWYzIiwidHlwIjoiYWNjZXNzIn0.WnWJM01meRDZRVIPr7tXqHcXSgrz0refMrpx7bMVgeQ",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODQxNDg3MTYsImlhdCI6MTU3ODk2NDcxNiwic3RpdGNoX2RhdGEiOm51bGwsInN0aXRjaF9kZXZJZCI6IjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsInN0aXRjaF9kb21haW5JZCI6IjVkZDgxYmJlNzUxYWM5NmQzNzZiNTI4NSIsInN0aXRjaF9pZCI6IjVlMWQxNmVjOGFjOTNkMzBhYzQ4NGU5NCIsInN0aXRjaF9pZGVudCI6eyJpZCI6IjVlMTNhNDFlMWIzOGQzNTg0MzBlZDFmMiIsInByb3ZpZGVyX3R5cGUiOiJsb2NhbC11c2VycGFzcyIsInByb3ZpZGVyX2lkIjoiNWUxM2E0MDUxYjM4ZDM1ODQzMGVkMWI3In0sInN1YiI6IjVlMTNhNDFlMWIzOGQzNTg0MzBlZDFmMyIsInR5cCI6InJlZnJlc2gifQ.fqr19MaUykKqi8C8csJUUzNe9jQOucPbtcc0soWgc5k"

Access tokens expire 30 minutes after MongoDB Realm grants them. When an access token expires, you can either request another access token using the user's credentials or use the refresh token to request a new access token with including the user's credentials.

The Client API session refresh endpoint accepts a POST request that includes the refresh token in the Authorization header and uses the following URL:
Beaker IconExample

The following request demonstrates how to use a refresh token to get a new, valid access token. Replace <Refresh Token> with the refresh_token value for the access token that you want to refresh.

curl --location --request POST '' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <Refresh Token>'
Give Feedback