# Use OpenStack from Python

> Connect with the OpenStack SDK for Python, reuse its session with other client libraries, and handle API errors.

This guide connects from Python with the OpenStack SDK, the standard Python library for
OpenStack services.

## Before you begin

You need Python 3.10 or later, and either a `clouds.yaml` entry from [Set up the CLI](/developer/cli)
or an [application credential](/identity/application-credentials).

## 1. Install the SDK

```sh
pip install openstacksdk
```

## 2. Connect

Connect with the same `clouds.yaml` entry the CLI uses:

```python
import openstack

conn = openstack.connect(cloud="vexxhost")
```

Or pass an application credential directly:

```python
import openstack

conn = openstack.connect(
    auth_type="v3applicationcredential",
    auth_url="https://auth.vexxhost.net/v3",
    application_credential_id="3c2b8e1f4a6d4c0e9b7a5f3d1e2c4b6a",
    application_credential_secret="Jq7mX2vL9pR4tW8yZ1bC5nF3hK6dS0gAuE2iO7qT4wY9rU1oP3aL6sD8fG5hJ2kZ0xC7vB4nM1qW8eR3tY6u",
    region_name="<region>",
)
```

The connection gets tokens and renews them when they expire, and it finds each service in the
[service catalog](/identity/service-catalog).

## 3. Make a call

```python
for project in conn.identity.user_projects(conn.current_user_id):
    print(project.id, project.name)
```

The SDK has a proxy for each OpenStack service, such as `conn.compute`, `conn.network` and
`conn.block_storage`.

## 4. Use the session with other clients

Some services have their own client library. Those clients take a keystoneauth session, and the
connection has one, already signed in:

```python
client = Client("1", session=conn.session)
```

Each product's documentation names its client library.

## 5. Handle errors

A failed request raises an exception from `openstack.exceptions`, such as `NotFoundException`,
`ForbiddenException` or `ConflictException`. Each carries the details of the response:

| Attribute     | Contents                                |
| ------------- | --------------------------------------- |
| `status_code` | The HTTP status.                        |
| `details`     | The error message the service returned. |
| `request_id`  | The request ID, for support.            |

```python
from openstack import exceptions

try:
    conn.identity.get_project("7d3e9b1c5a2f4e8d9c6b0a1f3e5d7c9b")
except exceptions.HttpException as error:
    print(error.status_code, error.request_id, error.details)
    raise
```

## Next steps

* Read the [OpenStack SDK documentation](https://docs.openstack.org/openstacksdk/latest/user/) for
  every service's calls.
* [Make API requests](/developer/api) to services directly.
