API

Extend Active Attribution Set

Extend the active attribution set in Alviss AI by adding a new attribution while preserving existing ones using the API.

This tutorial covers extending the active attribution set in Alviss AI by fetching the current active set, aligning dates with a new attribution ID, creating a new set, polling for completion, and optionally activating it. This is useful for updating attribution sets in ongoing projects to include new attributions while maintaining consistency. We'll walk through the process step by step using Python with the requests library to interact with the Alviss AI API. For each step, the relevant valid Python code snippet is provided.

Prerequisites

  • You need a valid access token from the Alviss AI platform (see the Authentication section in the main API docs).
  • Know your team ID and project ID.
  • Have a new attribution ID ready (e.g., from a previous attribution creation step; in the example, it's hardcoded as 5, but replace with your actual ID).
  • Install the required Python libraries if not already present: pip install requests (though it's standard).

Step 1: Import Necessary Libraries

Import time for handling delays in polling and requests for making HTTP API calls.

import time
import requests

Step 2: Set Up Variables

Define the base API URL, your access token, team ID, and project ID. Replace placeholders like <SET ME> with actual values.

url = "https://app.alviss.io/api/v1/api"
token = "<SET ME>"
team_id = "<SET ME>"
project_id = "<SET ME>"

Step 3: Prepare Authentication Headers

Create a headers dictionary with the Authorization Bearer token to authenticate all API requests.

headers = {"Authorization": "Bearer " + token}

Step 4: Construct the Project URL

Build the base URL for the team and project-specific endpoints by formatting the team ID and project ID into the URL string.

team_project_url = url + f"/projects/{team_id}/{project_id}"

Step 5: Get the Active Attribution Set

Send a GET request to the /attributionset/active endpoint to retrieve the current active attribution set. Extract the list of existing attribution IDs from the response.

response = requests.get(
    team_project_url + "/attributionset/active",
    headers=headers,
)
attribution_ids = [_attr["IId"] for _attr in response.json()["Attributions"]]

Step 6: Define the New Attribution ID

Specify the new attribution ID to add to the set. In this example, it's hardcoded as 5; replace with your actual new attribution ID.

attribution_id = 5

Step 7: Create a New Attribution Set by Aligning Dates

Send a POST request to the /attributionset_align_dates endpoint with the combined list of attribution job IDs (new and existing) in the JSON payload. Set "active": False to not activate immediately (change to True if desired). This returns a new attribution_set_id.

response = requests.post(
    team_project_url + "/attributionset_align_dates",
    headers=headers,
    json={
        "attributionjob_ids": [attribution_id, *attribution_ids],
        "active": False,  # set to True if you want to make this attribution set active
    },
)
attribution_set_id = response.json().get("IId")

Step 8: Poll for Attribution Set Completion

Use a while loop to repeatedly send GET requests to check the status of the new attribution set. Print progress messages and the response for debugging, and sleep for 2 seconds between checks. Break when the status is "complete".

while True:
    print("Waiting for attribution set to complete")
    response = requests.get(
        team_project_url + f"/attributionset/{attribution_set_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(2)

Step 9: Manually Activate the Attribution Set

If not activated during creation, send a PATCH request to the /attributionset/{attribution_set_id}/activate endpoint to make the new set active.

response = requests.patch(
    team_project_url + f"/attributionset/{attribution_set_id}/activate",
    headers=headers,
)

Full Example Code

import time
import requests

url = "https://app.alviss.io/api/v1/api"
token = "<SET ME>"
team_id = "<SET ME>"
project_id = "<SET ME>"

headers = {"Authorization": "Bearer " + token}

team_project_url = url + f"/projects/{team_id}/{project_id}"

# get active attribution set
response = requests.get(
    team_project_url + "/attributionset/active",
    headers=headers,
)
attribution_id = 5
attribution_ids = [_attr["IId"] for _attr in response.json()["Attributions"]]

response = requests.post(
    team_project_url + "/attributionset_align_dates",
    headers=headers,
    json={
        "attributionjob_ids": [attribution_id, *attribution_ids],
        "active": False,  # set to True if you want to make this attribution set active
    },
)
attribution_set_id = response.json().get("IId")
while True:
    print("Waiting for attribution set to complete")
    response = requests.get(
        team_project_url + f"/attributionset/{attribution_set_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(2)

# manually activate the attribution set
response = requests.patch(
    team_project_url + f"/attributionset/{attribution_set_id}/activate",
    headers=headers,
)