# Firebase Auth REST API

You can query the Firebase Auth backend through a REST API. This can be used for various operations such as creating new users, signing in existing ones, and editing or deleting these users.

Throughout this document, `API_KEY` refers to the Web API Key, which can be obtained on the [project settings](https://console.firebase.google.com/project/_/settings/general/) page in your admin console.

## API Usage
- **Exchange custom token for an ID and refresh token**  
You can exchange a custom Auth token for an ID and refresh token by issuing an HTTP `POST` request to the Auth `verifyCustomToken` endpoint.

**Method:** POST  
**Content-Type:** application/json  
**Endpoint**  
```
https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=[API_KEY]
```
**Request Body Payload**  
| Property Name | Type | Description |
| --- | --- | --- |
| token | string | A Firebase Auth custom token from which to create an ID and refresh token pair. |
| returnSecureToken | boolean | Whether or not to return an ID and refresh token. Should always be true. |

**Response Payload**  
| Property Name | Type | Description |
| --- | --- | --- |
| idToken | string | A Firebase Auth ID token generated from the provided custom token. |
| refreshToken | string | A Firebase Auth refresh token generated from the provided custom token. |
| expiresIn | string | The number of seconds in which the ID token expires. |

**Sample request**  
```
curl 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=[API_KEY]' \
-H 'Content-Type: application/json' \
--data-binary '{"token":"[CUSTOM_TOKEN]","returnSecureToken":true}'
```

A successful request is indicated by a `200 OK` HTTP status code. The response contains the Firebase ID token and refresh token associated with the custom token.

**Sample response**  
```
{
  "idToken": "[ID_TOKEN]",
  "refreshToken": "[REFRESH_TOKEN]",
  "expiresIn": "3600"
}
```

**Common error codes**  
- INVALID_CUSTOM_TOKEN: The custom token format is incorrect or the token is invalid for some reason (e.g. expired, invalid signature etc.)  
- CREDENTIAL_MISMATCH: The custom token corresponds to a different Firebase project.

### Exchange a refresh token for an ID token
You can refresh a Firebase ID token by issuing an HTTP `POST` request to the `securetoken.googleapis.com` endpoint.

**Method:** POST  
**Content-Type:** application/x-www-form-urlencoded  
**Endpoint**  
```
https://securetoken.googleapis.com/v1/token?key=[API_KEY]
```
**Request Body Payload**  
| Property Name | Type | Description |
| --- | --- | --- |
| grant_type | string | The refresh token's grant type, always "refresh_token". |
| refresh_token | string | A Firebase Auth refresh token. |

**Response Payload**  
| Property Name | Type | Description |
| --- | --- | --- |
| expires_in | string | The number of seconds in which the ID token expires. |
| token_type | string | The type of the refresh token, always "Bearer". |
| refresh_token | string | The Firebase Auth refresh token provided in the request or a new refresh token. |
| id_token | string | A Firebase Auth ID token. |
| user_id | string | The uid corresponding to the provided ID token. |
| project_id | string | Your Firebase project ID. |

**Sample request**  
```
curl 'https://securetoken.googleapis.com/v1/token?key=[API_KEY]' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data 'grant_type=refresh_token&refresh_token=[REFRESH_TOKEN]'
```

A successful request is indicated by a `200 OK` HTTP status code. The response contains the new Firebase ID token and refresh token.

**Sample response**  
```
{
  "expires_in": "3600",
  "token_type": "Bearer",
  "refresh_token": "[REFRESH_TOKEN]",
  "id_token": "[ID_TOKEN]",
  "user_id": "tRcfmLH7o2XrNELi...",
  "project_id": "1234567890"
}
```

**Common error codes**  
- TOKEN_EXPIRED: The user's credential is no longer valid. The user must sign in again.  
- USER_DISABLED: The user account has been disabled by an administrator.  
- USER_NOT_FOUND: The user corresponding to the refresh token was not found. It is likely the user was deleted.  
- API key not valid. Please pass a valid API key. (invalid API key provided)  
- INVALID_REFRESH_TOKEN: An invalid refresh token is provided.  
- Invalid JSON payload received. Unknown name \
