Port SQL queries into an Omni workbook via API
Requirements
To follow along with this guide, you’ll need:- An Omni API token
- To install
jq, which is a lightweight JSON processor. The script in this guide uses it to build and parse JSON payloads. Refer to the jq documentation for installation instructions. - The following information from your Omni instance:
- The subdomain of your Omni URL, for example
blobsrus - The ID of the model you want to query
- The subdomain of your Omni URL, for example
Implementation
The following script usescurl to send a POST request to the /api/v1/documents endpoint. It defines four distinct SQL queries and organizes them into a single workbook with specific visualization types: line charts, bar charts, and tables.
This part of the guide walks through different parts of the script and how they work. Refer to the Run the script section for a full copy-pasteable version of the script.
Script configuration
The first part of the script contains your Omni information, which you’ll need to define before running the script:
-
OMNI_INSTANCE- The subdomain of your Omni instance -
OMNI_API_TOKEN- Your Omni API key -
MODEL_ID- The ID of the model you want to query -
DOCUMENT_NAME- The name of the document you’ll create -
BASE_TABLE- The name of an existing view in your Omni model. This cannot be an arbitrary string.Each query can point to a different base table. The examples in this guide all reference the sameorder_itemstable for simplicity, but one query could point toorderswhile another points toproducts.Omni recommends pointing to a view that is contextually related to the query being defined. While the field primarily serves as a reference point, using an unrelated view may produce unexpected behavior.
Script configuration
SQL queries
The SQL queries section defines the actual SQL queries that will be ported into the workbook.
SQL queries
API request
The API request portion of the script uses the information in the Configuration and SQL queries sections to build the curl request.The
queryPresentations array defines the individual query tabs (tiles) within the workbook. Each query tab object contains a few parameters that are worth highlighting:isSql: true- This parameter tells Omni to treat theuserEditedSQLstring as the primary source of truth for the queryvisConfig- This parameter sets the default visualization style for the query tab, such asline,bar, ortable
API request
Run the script
The following code example is a full copy-pasteable version of the script. To use it:- Save this version to a convenient location.
- Replace the configuration variables with your Omni information.
- Use the command line to run the script!
Full copy-pasteable script version
Next steps
Once you run the script, the API will return a JSON object containing aworkbook identifier. You can navigate to https://[instance].omniapp.co/dashboards/[identifier] to view your new multi-tab analysis.