Make API Call to run job

B_J_Innov
New Contributor III

Hi everyone,

I want to trigger a run for a job using API Call.

Here's my code

"""

import shlex
import subprocess

def call_curl(curl):
args = shlex.split(curl)
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
return stdout, stderr

if __name__ == '__main__':
curl = '''curl --netrc --request POST \
https://<databricks-instance>/api/2.1/jobs/run-now \
--data {"job_id": 123456}
\
| jq .
'''
stdout, stderr = call_curl(curl)
print(stdout)
print(stderr)"""

This returns : """

b'<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>\n<title>Error 401 Unauthorized</title>\n</head>\n<body><h2>HTTP ERROR 401</h2>\n<p>Problem accessing /api/2.0/jobs/run-now. Reason:\n<pre>    Unauthorized</pre></p>\n</body>\n</html>\n'
b'  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100   266  100   258  100     8   1601     49 --:--:-- --:--:-- --:--:--  1612\ncurl: (3) [globbing] unmatched close brace/bracket at pos 16\ncurl: (6) Could not resolve host: |; Unknown error\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: jq; Unknown error\ncurl: (6) Could not resolve host: .; Unknown error\n'
Script duration: 0.8s.

"""

I understand that I need to put credentials of some sort somewhere but in the documentation I checked, it doesn't show where or how I should do it.

https://docs.databricks.com/en/workflows/jobs/jobs-2.0-api.html#run-now

Someone already did this ? I would appreciate if someone could help me.

 

Thanks