> ## Documentation Index
> Fetch the complete documentation index at: https://docs.alvys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication and Global Permissions

> Configure Power BI parameters, retrieve an Alvys API access token, and set global permissions so subsequent queries authenticate against the Public API.

To interact with the Alvys API, you must first retrieve an access token using your API credentials. This token is required for authenticating all subsequent API queries.

## Authentication

### **Create an Access Token Query**:

* Open Power BI Desktop and go to **Home > Transform Data**.

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/04506be861f8.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=d4b3b3e0a29ad43869ffc4029e2756bb" alt="" width="1457" height="243" data-path="images/migrated/04506be861f8.png" />

**Setup API Credentials in Manage Parameters**

* In Power BI, go to the top ribbon, select **Transform Data > Manage Parameters**.
* **Ensure You Have the Following Parameters**:
  * `client_id`: The client ID provided by Alvys.
  * `client_secret`: The client secret provided by Alvys.
* **Create New Parameters if Not Present**:
  * If these parameters are not already available:
    * Click **New** to create a new parameter.
    * Use the exact names: `client_id` and `client_secret`.
* Replace the **Current Value** of each parameter with your respective credentials.

> ❗️ Important: Ensure the parameter names match exactly as defined here. If you use different names, you must update them in the `AccessToken` query accordingly.

* Once you've entered your credentials, click **OK** and then **Close & Apply** to save the changes.

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/5bab4fc476dd.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=253e182ec24f6a814b65fd8e806ac727" alt="" width="1010" height="972" data-path="images/migrated/5bab4fc476dd.png" />

<br />

* **Add a New Blank Query**:

To add a new query, you have two options:

1. **From the Ribbon**: Go to **Home > New Source > Blank Query**.

   <img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/a5f24fbdacf1.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=ca0ddf7629257f9d4c27e27024f00b46" alt="" width="313" height="585" data-path="images/migrated/a5f24fbdacf1.png" />

   <br />

2. **From the Queries Pane**: Right-click on any existing query (or in the empty space under the list of queries) and select **New Query > Blank Query**.

   <img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/db408f83a5f0.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=237f0f91dbbb009376f4237e3b186da5" alt="" width="442" height="522" data-path="images/migrated/db408f83a5f0.png" />

   <br />

<br />

1. Rename the query to `AccessToken`. In the **Queries Pane**, right-click on the newly created query and select **Rename**.

> ❗️ 📢 **Use exactly this name. The name will be referenced by other queries that require this token for authentication.**

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/713d2401dd30.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=ecb444b5593e5fc8aca55547c4efb0be" alt="" width="725" height="654" data-path="images/migrated/713d2401dd30.png" />

<br />

* In the Advanced Editor, add the code below.

```
let  
    // Define the token endpoint  
    url = "https://auth.alvys.com/oauth/token",

// URL-encode the client_secret, NOTE: should be used parameters Name for client_secret)
    
encodedClientSecret = Uri.EscapeDataString(client_secret),

// Construct the form data as a URL-encoded string
formData = "client_id=" & client_id & "&" &
           "client_secret=" & encodedClientSecret & "&" &
           "audience=" & Uri.EscapeDataString("https://api.alvys.com/public/") & "&" &
           "grant_type=client_credentials",

// Make the POST request
binaryFormData = Text.ToBinary(formData),
response = Web.Contents(url, [
    Headers = [
        #"Content-Type" = "application/x-www-form-urlencoded"
    ],
    Content = binaryFormData
]),

// Parse the JSON response to extract the token
jsonResponse = Json.Document(response),
accessToken = jsonResponse[access_token]

in  
    accessToken
```

Save and close the query.

* **Expected Result**: The `AccessToken` query will return the `accessToken`, which can be dynamically used in other API queries.

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/7bc0c1b02ac8.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=899192447ddbcac4ec52731afbf4cf7a" alt="" width="1690" height="372" data-path="images/migrated/7bc0c1b02ac8.png" />

<br />

### Authentication and Global Permissions

The first time you attempt to connect to the Alvys API in Power BI, you will be prompted to authenticate. Follow these steps to configure the required settings:

1. Choose **Anonymous** for the Web API connection.
2. **Click Connect.** Once these settings are selected, click **Connect** to proceed.

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/da49461608a4.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=b449e7d29c7fc2d5f80b039353bb8d4d" alt="" width="1161" height="415" data-path="images/migrated/da49461608a4.png" />

3. **Privacy Level**: Click on the Data source settings > Global permissions > Edit permissions > in the Privacy Level dropdown, ensure the privacy level is set appropriately (e.g., Organizational or Public) for your environment.

<img src="https://mintcdn.com/alvys/MPqgFq1pceM5E4R4/images/migrated/8102ec13f453.png?fit=max&auto=format&n=MPqgFq1pceM5E4R4&q=85&s=2385905cb3cfce634ca3e68d6799f0ea" alt="" width="1256" height="900" data-path="images/migrated/8102ec13f453.png" />

<br />

> Note: You might be prompted to repeat this process for other endpoints if they are accessed for the first time. Ensure the same settings are applied for consistency across all queries.
