# Triggering a dbt Cloud Job in your automated workflow with python

**URL:** <https://discourse.getdbt.com/t/triggering-a-dbt-cloud-job-in-your-automated-workflow-with-python/2573>\
**Category:** Show and Tell\
**Tags:** orchestration-and-deployment, dbt-cloud\
**Created:** [May 27, 2021, 8:24am UTC](https://discourse.getdbt.com/t/triggering-a-dbt-cloud-job-in-your-automated-workflow-with-python/2573 "2021-05-27T08:24:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![boxysean](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/boxysean/32/617_2.png) [@boxysean](https://discourse.getdbt.com/u/boxysean)\
**Post date:** [May 27, 2021, 8:24am UTC](https://discourse.getdbt.com/t/triggering-a-dbt-cloud-job-in-your-automated-workflow-with-python/2573/1 "2021-05-27T08:24:18Z")

</div>

Say you want to create an automated workflow that includes a dbt Cloud Job. Maybe the workflow is within an orchestration tool like Airflow, Azure Data Factory, or AWS Step Functions. Or perhaps you want a dbt Cloud Job to be part of a git change management process powered by GitHub Actions, CircleCI, or Azure DevOps. How can you do that?

Well, that’s where the [dbt Cloud API](https://docs.getdbt.com/dbt-cloud/api) comes in! The [`trigger job to run`](https://docs.getdbt.com/dbt-cloud/api#operation/triggerRun) endpoint can be accessed to run a Job of your choosing. Then, you can use the [`get run`](https://docs.getdbt.com/dbt-cloud/api#operation/getRunById) endpoint to implement a basic and effective API polling technique to check the Job Run status.

I prefer a python script for this solution as it’s a powerful language understood by many developers, with exceptional error handling capabilities. Below is a basic script I recommend starting with:

```auto
import enum
import os
import time

# Be sure to `pip install requests` in your python environment
import requests

ACCOUNT_ID = <your-dbt-cloud-account-id>
JOB_ID = <your-dbt-cloud-account-id>

# Store your dbt Cloud API token securely in your workflow tool
API_KEY = os.getenv('DBT_CLOUD_API_TOKEN')

# These are documented on the dbt Cloud API docs
class DbtJobRunStatus(enum.IntEnum):
    QUEUED = 1
    STARTING = 2
    RUNNING = 3
    SUCCESS = 10
    ERROR = 20
    CANCELLED = 30

def _trigger_job() -> int:
    res = requests.post(
        url=f"https://cloud.getdbt.com/api/v2/accounts/{ACCOUNT_ID}/jobs/{JOB_ID}/run/",
        headers={'Authorization': f"Token {API_KEY}"},
        json={
            # Optionally pass a description that can be viewed within the dbt Cloud API.
            # See the API docs for additional parameters that can be passed in,
            # including `schema_override` 
            'cause': f"Triggered by my workflow!",
        }
    )

    try:
        res.raise_for_status()
    except:
        print(f"API token (last four): ...{API_KEY[-4:]}")
        raise

    response_payload = res.json()
    return response_payload['data']['id']

def _get_job_run_status(job_run_id):
    res = requests.get(
        url=f"https://cloud.getdbt.com/api/v2/accounts/{ACCOUNT_ID}/runs/{job_run_id}/",
        headers={'Authorization': f"Token {API_KEY}"},
    )

    res.raise_for_status()
    response_payload = res.json()
    return response_payload['data']['status']

def run():
    job_run_id = _trigger_job()

    print(f"job_run_id = {job_run_id}")

    
    while True:
        time.sleep(5)

        status = _get_job_run_status(job_run_id)

        print(f"status = {status}")

        if status == DbtJobRunStatus.SUCCESS:
            break
        elif status == DbtJobRunStatus.ERROR or status == DbtJobRunStatus.CANCELLED:
            raise Exception("Failure!")

if __name__ == ' __main__':
    run()

```

### Ideas for using this script

- Triggering a Continuous Integration job when you open a pull request in BitBucket or Azure DevOps. (Note: with GitHub and GitLab, [this can be done in dbt Cloud with the `Run On Pull Request` feature!](https://docs.getdbt.com/docs/dbt-cloud/using-dbt-cloud/cloud-enabling-continuous-integration-with-github#enabling-continuous-integration-ci))
- Triggering a Production job when you merge a pull request in to the `main` branch.

### Related topics

- @matt.winkler [describes](https://discourse.getdbt.com/t/orchestrating-fivetran-and-dbt-with-airflow/2079) how to use Airflow to orchestrate dbt Cloud and Fivetran.
- @DAVOS [shows us](https://discourse.getdbt.com/t/trigger-a-dbt-cloud-job-on-merging-to-master-branch/1278) how to use `curl` commands to trigger the dbt Cloud API within CircleCI.

---

<div class="post-metadata">

**Author:** ![ernesto](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/ernesto/32/32_2.png) [@ernesto](https://discourse.getdbt.com/u/ernesto)\
**Post date:** [September 9, 2021, 4:48pm UTC](https://discourse.getdbt.com/t/triggering-a-dbt-cloud-job-in-your-automated-workflow-with-python/2573/2 "2021-09-09T16:48:42Z")

</div>

Thanks for this @boxysean! I’ve created a [Google Colab notebook](https://gist.github.com/ernestoongaro/657689941acbf46d71f77b88627317bb) from your code for quick testing.
