Skip to content

Instantly share code, notes, and snippets.

@jeremyyeo
Last active February 24, 2022 11:08
Show Gist options
  • Save jeremyyeo/38f7025e1c3aa07fe5d0631c5c6fe222 to your computer and use it in GitHub Desktop.
Save jeremyyeo/38f7025e1c3aa07fe5d0631c5c6fe222 to your computer and use it in GitHub Desktop.
Creating jobs with the dbt Cloud API #dbt

Creating jobs with the dbt Cloud API

As of 2021-12-03, the docs for the "Create job" end point does not reflect the reality of what is actually required in the POST request payload (see relevant github issue).

See attached main.py for an example of a working payload - all the keys in the example payload must be included (e.g. "id", "triggers", "schedule") even though they are not specied as required in the API docs.

import requests
DBT_CLOUD_API_KEY = "<your-dbt-cloud-api-key>"
BASE_URL = "https://cloud.getdbt.com/api/v2"
ACCOUNT_ID = 123
PROJECT_ID = 123
ENVIRONMENT_ID = 123
headers = {
"Content-Type": "application/json",
"Authorization": f"Token {DBT_CLOUD_API_KEY}",
}
payload = {
"id": None,
"account_id": ACCOUNT_ID,
"project_id": PROJECT_ID,
"environment_id": ENVIRONMENT_ID,
"name": "Scheduled Job",
"execute_steps": ["dbt run"],
"state": 1,
"dbt_version": "0.21.0",
"triggers": {
"schedule": False,
"custom_branch_only": False,
"github_webhook": False,
},
"settings": {"threads": 1, "target_name": "default"},
"schedule": {
"date": {"type": "every_day"},
"time": {"type": "every_hour", "interval": 1},
},
}
r = requests.post(
url=f"{BASE_URL}/accounts/{ACCOUNT_ID}/jobs/", headers=headers, json=payload
)
try:
r.raise_for_status()
except Exception as e:
print(e)
resp = f"status: {r.json()['status']['code']} - job_id: {r.json()['data']['id']} - job_name: {r.json()['data']['name']}"
print(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment