API

Upload Data

Upload a file and create a dataset in Alviss AI using the API then activate it for immediate use.

This tutorial covers uploading a file, creating a dataset from it, and activating the dataset. This is useful for initializing data in Alviss AI projects. 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 the file you want to upload ready (e.g., a CSV or other supported format for datasets like "Sales").
  • 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, project ID, and the local path to the file you want to upload. Replace placeholders like <set me> with actual values.

url = "https://app.alviss.io/api/v1/api"
token = "<set me>"  # access token created in https://app.alviss.io/-/user
team_id = "<set me>"
project_id = "<set me>"
file_path = "<set me>"  # the file we want to upload

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: Upload the File

Send a POST request to the /datauploads endpoint. Include the dataset name (e.g., "Sales") as a query parameter and attach the file using the files parameter. This initiates the upload and returns an upload_id in the response.

response = requests.post(
    team_project_url + "/datauploads",
    headers=headers,
    params={"dataset_name": "Sales"},  # the file type: [Sales,Events, Media, Extra, .....]
    files={"file": open(file_path, "rb")},
)
upload_id = response.json().get("upload_id")

Step 6: Poll for Upload Completion

Use a while loop to repeatedly send GET requests to check the status of the upload using the upload_id. Print the response for debugging and sleep for 1 second between checks. Break when the status is "complete".

while True:
    response = requests.get(
        team_project_url + f"/datauploads/{upload_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(1)

Step 7: Create the Dataset

Send a POST request to the /datasets endpoint with the upload_id in the JSON payload. Optionally, you can activate it immediately or extend an existing dataset by uncommenting the relevant lines. This returns a dataset_id.

response = requests.post(
    team_project_url + "/datasets",
    headers=headers,
    json={
        "upload_ids": [upload_id],
        # "activate": True,  # if one want to activate the dataset right away
        # "dataset_ids": [8],  # used to extend a dataset
    },
)
dataset_id = response.json().get("IId")

Step 8: Poll for Dataset Completion

Similar to Step 6, poll the dataset's status using GET requests until it's "complete".

while True:
    response = requests.get(
        team_project_url + f"/datasets/{dataset_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(1)

Step 9: Activate the Dataset

If not activated during creation, send a POST request to the /datasets/{dataset_id}/activate endpoint to make the dataset active for use.

response = requests.post(
    team_project_url + f"/datasets/{dataset_id}/activate",
    headers=headers,
)

Full Example Code

"""
Script to upload files and create a dataset in Alviss AI.

See https://api.alviss.io/v1/api/docs#/ for the API documentation.
"""
import time
import requests

url = "https://app.alviss.io/api/v1/api"
token = "<set me>"  # access token created in https://app.alviss.io/-/user
team_id = "<set me>"
project_id = "<set me>"
file_path = "<set me>"  # the file we want to upload

headers = {"Authorization": "Bearer " + token}
team_project_url = url + f"/projects/{team_id}/{project_id}"

# Create a data upload
response = requests.post(
    team_project_url + "/datauploads",
    headers=headers,
    params={"dataset_name": "Sales"},  # the file type: [Sales,Events, Media, Extra, .....]
    files={"file": open(file_path, "rb")},
)

# wait for the dataupload to be completed
upload_id = response.json().get("upload_id")
while True:
    response = requests.get(
        team_project_url + f"/datauploads/{upload_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(1)

# create the dataset
response = requests.post(
    team_project_url + "/datasets",
    headers=headers,
    json={
        "upload_ids": [upload_id],
        # "activate": True,  # if one want to activate the dataset right away
        # "dataset_ids": [8],  # used to extend a dataset
    },
)
dataset_id = response.json().get("IId")

# wait for the dataupload to complete
while True:
    response = requests.get(
        team_project_url + f"/datasets/{dataset_id}",
        headers=headers,
    )
    if response.json().get("Status") == "complete":
        break
    print(response.json())
    time.sleep(1)

# activate the dataset
response = requests.post(
    team_project_url + f"/datasets/{dataset_id}/activate",
    headers=headers,
)