## Generate authorization token for local user

### cURL

```bash
curl --request POST \
  --url https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login \
  --header 'Content-Type: application/json' \
  --header 'x-site-context: <x-site-context>' \
  --data '\n{\n  "accountId": 4781348886,\n  "username": "john@fabric.inc",\n  "password": "joHn@123456789!"\n}'
```

### Python

```python
import requests

url = "https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login"

payload = {
    "accountId": 4781348886,
    "username": "john@fabric.inc",
    "password": "joHn@123456789!"
}
headers = {
    "x-site-context": "<x-site-context>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

### JavaScript

```javascript
const options = {
  method: 'POST',
  headers: {'x-site-context': '<x-site-context>', 'Content-Type': 'application/json'},
  body: JSON.stringify({
    accountId: 4781348886,
    username: 'john@fabric.inc',
    password: 'joHn@123456789!'
  })
};

fetch('https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### PHP

```php
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",\
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'accountId' => 4781348886,
    'username' => 'john@fabric.inc',
    'password' => 'joHn@123456789!'
  ]),
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/json",
    "x-site-context: <x-site-context>"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>
```

### Go

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

url := "https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login"

payload := strings.NewReader("{\n  \"accountId\": 4781348886,\n  \"username\": \"john@fabric.inc\",\n  \"password\": \"joHn@123456789!\"\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("x-site-context", "<x-site-context>")
	req.Header.Add("Content-Type", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))
}
```

### Java

```java
HttpResponse<String> response = Unirest.post("https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login")
  .header("x-site-context", "<x-site-context>")
  .header("Content-Type", "application/json")
  .body("{\n  \"accountId\": 4781348886,\n  \"username\": \"john@fabric.inc\",\n  \"password\": \"joHn@123456789!\"\n}")
  .asString();
```

### Ruby

```ruby
require 'uri'
require 'net/http'

url = URI("https://prod01-apigw.yourcompany.fabric.zone/api-identity/auth/local/login")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["x-site-context"] = '<x-site-context>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"accountId\": 4781348886,\n  \"username\": \"john@fabric.inc\",\n  \"password\": \"joHn@123456789!\"\n}"

response = http.request(request)
puts response.read_body
```

### Responses

#### Success (200)
```json
{
  "_id": "60c3a2c476f9c21239d836ca",
  "account": {
    "accountId": 8739392294
  },
  "roles": [
    "Admin"
  ],
  "permissions": [
    "read:user"
  ],
  "refreshToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVC.eyJpZCI6IjYwYzNhMmM0....",
  "accessToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVC.eyJpZCI6IjYwYzNhMmM0...."
}
```

#### Bad Request (400)
```json
{
  "code": "BAD_REQUEST",
  "message": "Bad Request"
}
```

#### Internal Server Error (500)
```json
{
  "code": "INTERNAL_SERVER_ERROR",
  "message": "Internal server error."
}
```

### Headers

- **x-site-context** (object, required)  
  The x-site-context is a JSON object that must contain date, and channel attributes. eg - {"date":"2020-12-12T08:00:00.000Z","channel":12}

### Body

- **accountId** (number)  
  Merchant's 10 character copilot account ID  
  Example: `4781348886`

- **username** (string)  
  Username or email address of the user who will log in  
  Example: `"john@fabric.inc"`

- **password** (string)  
  Password using which you log into fabric copilot UI  
  Example: `"joHn@123456789!"`

### Response
- **200**: 
  - **_id** (string)  
    fabric internal ID  
    Example: `"60c3a2c476f9c21239d836ca"`  
  - **account** (object)  
    Copilot account information
    - **roles** (string[])  
      List of available roles for the user  
    - **permissions** (string[])  
      List of available permissions for the user  
    - **refreshToken** (object)  
      Refresh token
      Example: `"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVC.eyJpZCI6IjYwYzNhMmM0...."`  
    - **accessToken** (object)  
      Access Token. This is the token to consume the copilot APIs.  
      Example: `"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVC.eyJpZCI6IjYwYzNhMmM0...."`
