# 1. Authentication 

All APIs are authenticated via TWO-LEGGED OAUTH2 (2LO). This entails retrieving a short-lived OAuth 2.0 access token granted using application client credentials. The token is valid for authenticating requests to the API for one hour, after which a new token for future requests must be fetched.

## 1.1 URL

Please use the URL for an API to generate an access token for that API. Access tokens will not authenticate across different API URLs.

|API|Sandbox|Production|
|-|-|-|
|Alerts Management, RAD-E, Webhook Subscriptions|`https://api-sandbox.rapidsos.com`|`https://api.rapidsos.com`|


## 1.2 Obtaining Credentials
If you do not have the credentials you require, please submit a request via our support portal at [support.rapidsos.com](https://support.rapidsos.com) or by directly contacting us at support@rapidsos.com. We will securely share the credentials with you if approved within two business days.

## 1.3 Getting an access token
To create an access token, make an HTTP POST request to `/oauth/token` with your client credentials. You must provide the following attributes in the `x-www-form-urlencoded` format:
- **client_id** (string) - The client ID of your credentials
- **client_secret** (string) - The client secret of your credentials
- **grant_type** (string) - Should be hard coded to the value `“client_credentials”`.

A sample request for an access token looks as follows:
```http
POST /oauth/token HTTP/1.1
Host: api-sandbox.rapidsos.com
Content-Type: application/x-www-form-urlencoded;

client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=client_credentials
```

### Code Sample

Here is a python code sample demonstrating how to obtain your access tokens for the required APIs

> The below code sample uses the `Requests` HTTP library. More information about `Requests` can be found [here](https://pypi.org/project/requests/).
```python title="get_access_token.py" lineNumbers
import requests # HTTP library for python

# Your credentials
ALERTS_MGMT_URL = 'https://api-sandbox.rapidsos.com'
ALERTS_MGMT_CLIENT_ID = 'YOUR_CLIENT_ID'
ALERTS_MGMT_CLIENT_SECRET = 'YOUR_CLIENT_SECRET'

def get_access_token(base_url, client_id, client_secret):
    """Retrieves an access token for a URL given credentials."""

    auth_payload = {
        'client_id': client_id,
        'client_secret': client_secret,
        'grant_type': 'client_credentials'
    }
    response = requests.post(
        f'{base_url}/oauth/token',
        data=auth_payload
    )
    assert response.ok
    return response.json()['access_token']


# Make authentication request
print(
  get_access_token(
    ALERTS_MGMT_URL, 
    ALERTS_MGMT_CLIENT_ID, 
    ALERTS_MGMT_CLIENT_SECRET
  )
)
```


## 1.4 Authentication Response
- **access_token**: Short-lived OAuth 2.0 access token
- **token_type**: Token type descriptor, in this case Bearer Token
- **expires_in**: Number of seconds after the issued_at time that the access token will expire (defaults to 3600 seconds)

```json title = "Response Example" lineNumbers
{
    "access_token": "1A2B3c4D5e6F7G8h",
    "token_type": "Bearer",
    "expires_in": "3599"
}
```
### [Continue to Step 2: Alerts Initialization](https://rapidsos.portal.swaggerhub.com/alerts/default/alerts-initialization)
