Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redash Refresh API usage example with parameters Raw
import os
import requests
import time
from pprint import pprint
def poll_job(s, redash_url, job):
# TODO: add timeout
while job['status'] not in (3,4):
response = s.get('{}/api/jobs/{}'.format(redash_url, job['id']))
job = response.json()['job']
time.sleep(1)
if job['status'] == 3:
return job['query_result_id']
return None
def get_fresh_query_result(redash_url, query_id, api_key, params):
s = requests.Session()
s.headers.update({'Authorization': 'Key {}'.format(api_key)})
response = s.post('{}/api/queries/{}/refresh'.format(redash_url, query_id), params=params)
if response.status_code != 200:
raise Exception('Refresh failed.')
result_id = poll_job(s, redash_url, response.json()['job'])
if result_id:
response = s.get('{}/api/queries/{}/results/{}.json'.format(redash_url, query_id, result_id))
if response.status_code != 200:
raise Exception('Failed getting results.')
else:
raise Exception('Query execution failed.')
return response.json()['query_result']['data']['rows']
if __name__ == '__main__':
params = {'p_param': 1243}
query_id = 1234
# Need to use a *user API key* here (and not a query API key).
api_key = '...'
pprint(get_fresh_query_result('https://app.redash.io/acme', query_id, api_key, params))
@Salbinus

This comment has been minimized.

Copy link

@Salbinus Salbinus commented Jun 7, 2019

Hi,
I've expected the response to be the query result in json format. Instead, I got the html of the UI (which results in a JSONDecodeError).
Just made the adjustment for the parameters (ID's and URI).
What I have to do to get the expected output?

@kishandonepudi

This comment has been minimized.

Copy link

@kishandonepudi kishandonepudi commented Jul 30, 2019

Hi,
I've expected the response to be the query result in json format. Instead, I got the html of the UI (which results in a JSONDecodeError).
Just made the adjustment for the parameters (ID's and URI).
What I have to do to get the expected output?

I had this same issue, can you pls let know incase you resolved it

@Salbinus

This comment has been minimized.

Copy link

@Salbinus Salbinus commented Jul 30, 2019

Hi,
I've expected the response to be the query result in json format. Instead, I got the html of the UI (which results in a JSONDecodeError).
Just made the adjustment for the parameters (ID's and URI).
What I have to do to get the expected output?

I had this same issue, can you pls let know incase you resolved it

I didn't solved this but my information is that the company I'm working for is blocking the API due to security concerns.

@ashi-taka

This comment has been minimized.

Copy link

@ashi-taka ashi-taka commented Sep 27, 2019

How do I find the "user API"? Replicating the above code to send the refresh query result on email.

@aborruso

This comment has been minimized.

@Salbinus

This comment has been minimized.

Copy link

@Salbinus Salbinus commented Oct 22, 2019

@aborruso It should be the 4 digit query ID which you can find in any URL. e.g. "redash.example.domain/queries/->4 digit number<-"

@aborruso

This comment has been minimized.

Copy link

@aborruso aborruso commented Oct 22, 2019

@Salbinus but in the script there is 1243 and also 1234.

@Salbinus

This comment has been minimized.

Copy link

@Salbinus Salbinus commented Oct 22, 2019

@aborruso ,good Point. havn't recognized this. I'm not absolutely Sure but it seems to be the Parameter , you can add to any query for reusability, See here: https://redash.io/help/user-guide/querying/query-parameters
If so, it should be optional.

@susodapop

This comment has been minimized.

Copy link

@susodapop susodapop commented Oct 22, 2019

@aborruso The value 1243 is a query parameter. The value 1234 is a query_id. These are just dummy numbers. If it helps, acme isn't a real organization 😛. To run this script, you would replace them with real values. Also, you can add more than one parameter. It depends how many parameters your query requires.

@aborruso

This comment has been minimized.

Copy link

@aborruso aborruso commented Oct 22, 2019

@susodapop my goal will be to work in ACME !!

Thank you ;)

@czen

This comment has been minimized.

Copy link

@czen czen commented Jun 10, 2020

Well this works just fine with 2 year old installation of redash, which is rather unexpected for some random gist on github.
Thanks!

@susodapop

This comment has been minimized.

Copy link

@susodapop susodapop commented Jun 12, 2020

@czen this "random gist on github" was written by the founder of Redash 😀

Nowadays you can also use redash-toolbelt to do this and many other operations as well. It's the officially maintained Redash API Python client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.