[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Apache Airflow™](../index.md) > [Tutorials](index.md) > Other tutorials > Sending requests to the Yandex Cloud API via the Yandex Cloud Python SDK

# Sending requests to the Yandex Cloud API via the Yandex Cloud Python SDK

# Sending requests to the Yandex Cloud API via the Yandex Cloud Python SDK

{% note warning %}

This tutorial was tested on clusters with the Apache Airflow™ version below 3.0.

{% endnote %}

When working with Managed Service for Apache Airflow™, you can use the [Yandex Cloud Python SDK](https://github.com/yandex-cloud/python-sdk) to make requests to the Yandex Cloud API. The service supports sending requests to any type of cloud resources. You do not need to set up authentication in the cloud manually. The Yandex Cloud Python SDK gets authenticated via the [IAM token](../../iam/concepts/authorization/iam-token.md) of the service account attached to the Apache Airflow™ cluster.

As an example, we use a [directed acyclic graph (DAG)](../concepts/index.md#about-the-service) to send a request to the Yandex Cloud API. The request returns a list of virtual machines in the folder where the Apache Airflow™ cluster was created.

To use the Yandex Cloud Python SDK to send requests to the Yandex Cloud API:

1. [Set up your infrastructure](#create-infrastracture).
1. [Prepare the DAG file and run the graph](#dag).
1. [Check the result](#check-result).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Managed Service for Apache Airflow™ cluster: computing resources of cluster components (see [Managed Service for Apache Airflow™ pricing](../pricing.md)).
* Yandex Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* VM instance, which includes the use of computing resources, storage, public IP address, and OS (see [Yandex Compute Cloud pricing](../../compute/pricing.md)).


## Set up your infrastructure {#create-infrastructure}

1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `airflow-sa` with the following roles:

   * `compute.viewer`
   * `managed-airflow.integrationProvider`

1. [Create a Object Storage bucket](../../storage/operations/buckets/create.md) in any configuration.

1. [Edit the ACL](../../storage/operations/buckets/edit-acl.md) of the new bucket to give the `READ` permission to the `airflow-sa` service account.

1. [Create a Managed Service for Apache Airflow™ cluster](../operations/cluster-create.md#create-cluster) with the following parameters:

   * **Service account**: `airflow-sa`
   * **Bucket name**: Name of the new bucket

1. [Create a VM](../../compute/operations/vm-create/create-linux-vm.md) in any configuration.

## Prepare the DAG file and run the graph {#dag}

1. Create a local file named `test_python_sdk.py` and paste the following script to it:

   {% cut "test_python_sdk.py" %}

   ```python
   from airflow import DAG
   from airflow.models import Connection
   from airflow.operators.python_operator import PythonOperator
   from datetime import datetime

   import yandexcloud
   from yandex.cloud.compute.v1.instance_service_pb2 import (
       ListInstancesRequest,
   )
   from yandex.cloud.compute.v1.instance_service_pb2_grpc import InstanceServiceStub

   def list_instances():
       conn = Connection.get_connection_from_secrets("yandexcloud_default")
       folder_id = conn.extra_dejson.get('folder_id')

       sdk = yandexcloud.SDK()
       instance_service = sdk.client(InstanceServiceStub)
       response = instance_service.List(ListInstancesRequest(folder_id=folder_id))
       print("instances: ", response.instances)

   with DAG(
       dag_id='test_python_sdk',
       start_date=datetime(2024, 5, 24),
       schedule="@once",
       catchup=False,
   ) as dag:
       PythonOperator(
           task_id='list_instances',
           python_callable=list_instances,
       )
   ```

   {% endcut %}

   You get authenticated in the cloud using the IAM token of the service account attached to the Apache Airflow™ cluster. The `yandexcloud.SDK()` object with default parameters is automatically populated with the data required to get authenticated using the IAM token.

1. [Upload](../../storage/operations/objects/upload.md) the `test_python_sdk.py` DAG file to the bucket you created earlier. This will automatically create a graph with the same name in the Apache Airflow™ web interface.

1. [Open the Apache Airflow™ web interface](../operations/af-interfaces.md#web-gui).

1. Make sure a new graph named `test_python_sdk` has appeared in the **DAGs** section.

   It may take a few minutes to load a DAG file from the bucket.

1. To run the graph, click ![image](../../_assets/managed-airflow/trigger-dag.png =18x) in the line with its name.

## Check the result {#check-result}

To check the result in the Apache Airflow™ web interface:

1. In the **DAGs** section, open the `test_python_sdk` graph.
1. Go to the **Grid** section.
1. Select **list_instances**.
1. Go to **Logs**.
1. Make sure the logs list the virtual machines from the folder where the Apache Airflow™ cluster was created. This means the query was successful.

## Delete the resources you created {#clear-out}

Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:

1. [Service account](../../iam/operations/sa/delete.md)
1. [Object Storage bucket](../../storage/operations/buckets/delete.md)
1. [Managed Service for Apache Airflow™ cluster](../operations/cluster-delete.md#delete)
1. [Virtual machine](../../compute/operations/vm-control/vm-delete.md)