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

# Get assets with outstanding tasks

## Introduction

Efficiently managing tasks and their associated assets is a critical requirement in workflows involving digital asset management systems. This guide demonstrates how to automate the process of finding tasks with a status that is not "Completed" and retrieving their corresponding assets by their IDs. By following these examples, you can streamline your task and asset management processes and integrate them into your existing workflows.

Authentication is a critical step in interacting with the Mudstack API. Each request requires an authorization token, which is obtained using your API key and secret. Additionally, the `x-workspace-id` and `x-account-id` headers must be included in every request to ensure proper scoping.

This example is particularly useful for scenarios such as:

* Identifying tasks that require immediate attention.
* Automating the retrieval of assets associated with pending tasks.
* Integrating task and asset management with other systems or workflows.

This example demonstrates how to:

1. Authenticate and retrieve an authorization token.
2. Retrieve tasks using the [`/tasks`](../workspaces/tasks/get-tasks) endpoint.
3. Filter tasks with a status that is not "Completed".
4. Retrieve the corresponding assets for the filtered tasks using the [`/workspaces/assets/{asset_id}`](../workspaces/assets/get-asset) endpoint.

## Steps

### Step 1: Authenticate and Retrieve an Authorization Token

Use the [`/auth/token`](../auth/authenticate-api-key) endpoint to retrieve an authorization token. Replace `<your_account_id>`, `<your_key>`, and `<your_secret>` with your account ID, API key, and secret, respectively.

<CodeGroup>
  ```python auth.py theme={null}
  # Python example to retrieve an authorization token
  import requests

  url = "https://api.mudstack.com/auth/token"
  headers = {
      "x-account-id": "<your_account_id>",
      "Content-Type": "application/json"
  }
  data = {
      "key": "<your_key>",
      "secret": "<your_secret>"
  }
  response = requests.post(url, headers=headers, json=data)
  token = response.json()["token"]
  print(token)
  ```

  ```javascript auth.js theme={null}
  // JavaScript example to retrieve an authorization token
  const fetch = require('node-fetch');

  const url = "https://api.mudstack.com/auth/token";
  const headers = {
      "x-account-id": "<your_account_id>",
      "Content-Type": "application/json"
  };
  const data = {
      key: "<your_key>",
      secret: "<your_secret>"
  };

  fetch(url, { method: "POST", headers, body: JSON.stringify(data) })
      .then(res => res.json())
      .then(data => console.log(data.token));
  ```

  ```csharp auth.cs theme={null}
  // C# example to retrieve an authorization token
  using System;
  using System.Net.Http;
  using System.Text;
  using System.Threading.Tasks;

  class Program {
      static async Task Main(string[] args) {
          var client = new HttpClient();
          var requestBody = "{\"key\": \"<your_key>\", \"secret\": \"<your_secret>\"}";
          var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
          content.Headers.Add("x-account-id", "<your_account_id>");
          var response = await client.PostAsync("https://api.mudstack.com/auth/token", content);
          var token = await response.Content.ReadAsStringAsync();
          Console.WriteLine(token);
      }
  }
  ```
</CodeGroup>

**Response Example:**

```json theme={null}
{
  "token": "your_generated_token"
}
```

### Step 2: Retrieve Tasks

Use the [`/tasks`](../workspaces/tasks/get-tasks) endpoint to retrieve tasks. Replace `<your_token>`, `<workspace_id>`, and `<account_id>` with your authorization token, workspace ID, and account ID, respectively.

<CodeGroup>
  ```python get-tasks.py theme={null}
  # Python example to retrieve tasks
  import requests

  url = "https://api.mudstack.com/tasks"
  headers = {
      "Authorization": "Bearer <your_token>",
      "x-workspace-id": "<workspace_id>",
      "x-account-id": "<account_id>"
  }
  response = requests.get(url, headers=headers)
  tasks = response.json()["tasks"]
  print(tasks)
  ```

  ```javascript get-tasks.js theme={null}
  // JavaScript example to retrieve tasks
  const fetch = require('node-fetch');

  const url = "https://api.mudstack.com/tasks";
  const headers = {
      "Authorization": "Bearer <your_token>",
      "x-workspace-id": "<workspace_id>",
      "x-account-id": "<account_id>"
  };

  fetch(url, { method: "GET", headers })
      .then(res => res.json())
      .then(data => console.log(data.tasks));
  ```

  ```csharp get-tasks.cs theme={null}
  // C# example to retrieve tasks
  using System;
  using System.Net.Http;
  using System.Threading.Tasks;

  class Program {
      static async Task Main(string[] args) {
          var client = new HttpClient();
          var request = new HttpRequestMessage(HttpMethod.Get, "https://api.mudstack.com/tasks");
          request.Headers.Add("Authorization", "Bearer <your_token>");
          request.Headers.Add("x-workspace-id", "<workspace_id>");
          request.Headers.Add("x-account-id", "<account_id>");

          var response = await client.SendAsync(request);
          Console.WriteLine(await response.Content.ReadAsStringAsync());
      }
  }
  ```
</CodeGroup>

**Response Example:**

```json theme={null}
{
  "tasks": [
    {
      "id": "task1",
      "status": "Pending",
      "asset_id": "asset1"
    },
    {
      "id": "task2",
      "status": "Completed",
      "asset_id": "asset2"
    }
  ]
}
```

### Step 3: Filter Tasks with Status Not "Completed"

Filter the tasks to exclude those with a status of "Completed".

<CodeGroup>
  ```javascript filter-tasks.js theme={null}
  // Filter tasks with status not "Completed"
  const filteredTasks = tasks.filter(task => task.status !== "Completed");
  ```

  ```python filter-tasks.py theme={null}
  # Python example to filter tasks
  filtered_tasks = [task for task in tasks if task["status"] != "Completed"]
  ```

  ```csharp filter-tasks.cs theme={null}
  // C# example to filter tasks
  var filteredTasks = tasks.Where(task => task.Status != "Completed").ToList();
  ```
</CodeGroup>

### Step 4: Retrieve Corresponding Assets

For each filtered task, retrieve the corresponding asset using the [`/workspaces/assets/{asset_id}`](../workspaces/assets/get-asset) endpoint.

<CodeGroup>
  ```python get-assets.py theme={null}
  # Python example to retrieve assets
  for task in filtered_tasks:
      url = f"https://api.mudstack.com/workspaces/assets/{task['asset_id']}"
      headers = {
          "Authorization": "Bearer <your_token>",
          "x-workspace-id": "<workspace_id>",
          "x-account-id": "<account_id>"
      }
      response = requests.get(url, headers=headers)
      print(response.json())
  ```

  ```javascript get-assets.js theme={null}
  // JavaScript example to retrieve assets
  filteredTasks.forEach(task => {
      const url = `https://api.mudstack.com/workspaces/assets/${task.asset_id}`;
      const headers = {
          "Authorization": "Bearer <your_token>",
          "x-workspace-id": "<workspace_id>",
          "x-account-id": "<account_id>"
      };
      fetch(url, { method: "GET", headers })
          .then(res => res.json())
          .then(console.log);
  });
  ```

  ```csharp get-assets.cs theme={null}
  // C# example to retrieve assets
  foreach (var task in filteredTasks) {
      var url = $"https://api.mudstack.com/workspaces/assets/{task.AssetId}";
      var request = new HttpRequestMessage(HttpMethod.Get, url);
      request.Headers.Add("Authorization", "Bearer <your_token>");
      request.Headers.Add("x-workspace-id", "<workspace_id>");
      request.Headers.Add("x-account-id", "<account_id>");

      var response = await client.SendAsync(request);
      Console.WriteLine(await response.Content.ReadAsStringAsync());
  }
  ```
</CodeGroup>

**Response Example:**

```json theme={null}
{
  "id": "asset1",
  "name": "example_asset.png",
  "status": "Pending"
}
```

***

## Explore More Examples

Looking for more examples? Check out the following guides to learn about other use cases:

* [Search Assets](../examples/searching-assets)
* [Update Status on Assets](../examples/update-status-on-assets)
* [Invite Users to Account](../examples/invite-users-to-account)
