how can i execute insights using api? i'm trying t...
# gooddata-cloud
r
how can i execute insights using api? i'm trying to warmup the cache again after invalidating it, which apis can i use for this?
k
You cannot “execute an insight” directly by ID via a dedicated Insight API in the materials provided, but you can warm up the cache by executing the same AFM query that the insight uses. From the docs, cache is filled whenever you execute analytics (dashboards, UI, or AFM API). After invalidation via upload notification, “Next time you open a visualization, it is re-computed with the latest data from the database, and the cache is populated with the recent data.” [[Reload cached data](https://www.gooddata.com/docs/cloud/connect-data/cache-management/reload-cached-data/#invalidate-cached-data); [FlexCache](https://www.gooddata.com/docs/cloud/connect-data/cache-management/#flexcache)] ### Step 1 – Invalidate cache (what you already do) Use the upload notification endpoint or Python SDK:
Copy code
bash curl $HOST_URL/api/v1/actions/dataSources/<data-source-id>/uploadNotification -X POST \   -H "Authorization: Bearer $API_TOKEN"
or
Copy code
python from gooddata_sdk import GoodDataSdk  sdk = GoodDataSdk.create(host, token) sdk.catalog_data_source.register_upload_notification(data_source_id="<data-source-id>")
[[Invalidate cache API](https://www.gooddata.com/docs/cloud/api-and-sdk/api/gooddata-cn-oapi-all-raw-schema.json); [Python register_upload_notification](https://www.gooddata.com/docs/python-sdk/latest/data/data-source/register_upload_notification/); [Reload cached data](https://www.gooddata.com/docs/cloud/connect-data/cache-management/reload-cached-data/)] ### Step 2 – Warm up cache by executing AFM To warm up cache you must execute queries. The available API for this is the AFM “computeReport” / result pair: 1. Execute endpoint – triggers computation and populates FlexCache:
Copy code
bash curl $HOST_URL/api/v1/actions/workspaces/<workspace_id>/execution/afm/execute \   -H 'Authorization: Bearer $API_TOKEN' \   -H 'Content-Type: application/json' \   -X POST \   -d '{         "resultSpec": { ... },         "execution":  { ... },         "settings":   {}       }'
This is
POST /api/v1/actions/workspaces/{workspaceId}/execution/afm/execute
. [[AFM example](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/#example); [computeReport method](https://www.gooddata.com/docs/cloud/api-and-sdk/api/gooddata-cn-oapi-all-raw-schema.json); [Access raw data](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/); [FlexCache – final query results](https://www.gooddata.com/docs/cloud/connect-data/cache-management/#flexcache)] 2. Result endpoint – fetches the computed data by
resultId
(also uses / populates cache). [[Access raw data](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/); [FlexCache – final query results](https://www.gooddata.com/docs/cloud/connect-data/cache-management/#flexcache)] ### How to mirror an insight’s query The sources here do not document an API that, given an insight ID, returns its AFM, so I cannot show a supported “execute insight by ID” REST call. The documented options are: - Use GoodData.UI hooks / components (
useInsightDataView
,
ExecuteInsight
, etc.) to execute an existing Analytical Designer visualization in a React app (which also warms the cache). [[Create custom visualizations](https://www.gooddata.com/docs/gooddata-ui/latest/learn/visualize_data/create_custom_visualizations/)] - Use the AFM API directly with a manually constructed AFM that matches the insight’s definition. [[Access raw data](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/)] In short: - Invalidate cache:
POST /api/v1/actions/dataSources/{dataSourceId}/uploadNotification
. - Warm cache: execute AFM queries via
POST /api/v1/actions/workspaces/{workspaceId}/execution/afm/execute
(or via GoodData.UI executions for your insights).
r
i have a list of insight ids only
@kapa.ai i have a list of insight ids only
k
A message from kapa.ai
There is no documented REST endpoint in the provided sources that lets you execute an insight directly by its ID (for example,
POST /insights/{insightId}/execute
). The documented computation APIs all work with AFM executions, not with insight IDs. [[Access raw data](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/)] What you can do based on the docs you shared: 1. Execute AFM directly to warm cache Use the AFM execute endpoint (this is what fills FlexCache): [[Access raw data – example](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/#example); [FlexCache](https://www.gooddata.com/docs/cloud/connect-data/cache-management/#flexcache)]
Copy code
bash    curl $HOST_URL/api/v1/actions/workspaces/<workspace_id>/execution/afm/execute \      -H 'Authorization: Bearer $API_TOKEN' \      -H 'Content-Type: application/json' \      -X POST \      -d '{            "resultSpec": { ... },            "execution":  { ... },            "settings":   {}          }'
In Python SDK this is
compute_report(workspace_id, afm_execution)
. [[compute_report](https://github.com/gooddata/gooddata-python-sdk/blob/master/gooddata-api-client/docs/ActionsApi.md)] 2. But you must already have the AFM for each insight The sources you provided do not describe any API like “get AFM by insight ID” or “execute insight by ID”, nor a way to transform an insight ID into an AFM payload on the server side. They only show: - How to run AFM (
compute_report
/
/execution/afm/execute
) [[Access raw data](https://www.gooddata.com/docs/cloud/api-and-sdk/api/access_raw_data_through_api/)] - How to debug AFM / SQL (
explain_afm
/
explainAFM
) [[explain_afm](https://github.com/gooddata/gooddata-python-sdk/blob/master/gooddata-api-client/docs/ActionsApi.md); [explainAFM](https://www.gooddata.com/docs/cloud/api-and-sdk/api/gooddata-cn-oapi-all-raw-schema.json)] - How to inspect an existing execution result by
resultId
(
retrieve_execution_metadata
) [[retrieve_execution_metadata](https://github.com/gooddata/gooddata-python-sdk/blob/master/gooddata-api-client/docs/ActionsApi.md); [retrieveExecutionMetadata](https://www.gooddata.com/docs/cloud/api-and-sdk/api/gooddata-cn-oapi-all-raw-schema.json)] Because of that, with only a list of insight IDs, the knowledge sources do not describe a supported way to: - Resolve those IDs to AFM definitions via REST, and then - Call
/execution/afm/execute
for each. So, within the scope of the provided documentation: - You can warm up cache only by executing AFM queries (or opening the insights in UI / GoodData.UI, which triggers those AFMs behind the scenes). [[FlexCache](https://www.gooddata.com/docs/cloud/connect-data/cache-management/#flexcache); [Reload cached data](https://www.gooddata.com/docs/cloud/connect-data/cache-management/reload-cached-data/)] - There is no documented API here to go from “insight ID → AFM” or “insight ID → execute” that you could use directly with just a list of IDs. If you need to proceed purely by API, you would need an additional, insight-related endpoint or SDK helper to load each insight definition and convert it to AFM, but this is not covered in the materials you shared.
r
can anyone else help here?
m
Hi Rajat, I am not sure if there is any API that allows to execute insight by its ID. But here seems to be such method in the GoodData Python SDK: https://www.gooddata.com/docs/python-sdk/latest/execution/exports/export_tabular_by_visualization_id/ Maybe that can be used?