> For the complete documentation index, see [llms.txt](https://api-docs.devhub.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://api-docs.devhub.com/code-examples/python.md).

# Python

## Python 3

Example using `requests_oauthlib` package

### Base oauth client

```python
import json
from requests_oauthlib import OAuth1Session

client = OAuth1Session('PROVIDEDKEYHERE',
                       client_secret='PROVIDEDSECRETHERE')
base_url = 'https://yourbrand.cloudfrontend.net/api/v2/'
```

### `GET` request

Example fetching a list of Sites.

```python
response = client.get(
    '{}sites/'.format(base_url),
    params={
        'deleted': 0,
    })
content = response.json()
```

### `POST` request

Creating a new site using a JSON object containing the payload.

```python
payload = {
    'formatted_domain': 'www.somebusinessname.com',
    ...
}
response = client.post(
    '{}sites/'.format(base_url),
    json=payload)
content = response.json()
```

## Python 2

Example using `oauth2` library

### `GET` request

```python
import json
import oauth2 as oauth

consumer = oauth.Consumer('PROVIDEDKEYHERE', 'PROVIDEDSECRETHERE')
client = oauth.Client(consumer, None)
response, content = client.request('/api/v2/sites/', 'GET', headers={
    'content-type': 'application/json'
})
content = json.loads(content)
```

### `POST` request

A JSON object containing the parameters is encoded as a string and then passed as the request body.

```python
import json
import oauth2 as oauth

consumer = oauth.Consumer('PROVIDEDKEYHERE', 'PROVIDEDSECRETHERE')
client = oauth.Client(consumer, None)
params = {
    'domain': 'somebusinessname.com',
    ...
}
response, content = client.request('/api/v2/sites/', 'POST', body=json.dumps(params), headers={
    'Content-Type': 'application/json'
})
content = json.loads(content)
```
