Your first script¶
By the end of this page you will have run one script that lists a few of your DataSets and then fetches one of them by its id. It only reads. It changes nothing in Domo.
Credentials stay in .env
The script reads your access token from a .env file, so the token is never
written in the code. Do not paste the token into the script, and do not
commit .env to git. If you have not made the .env file yet, do steps 1
and 2 of Authenticating to Domo first.
The script¶
You need crew-dcs installed, and a .env file next to the script with
DOMO_INSTANCE (the mycompany in mycompany.domo.com) and
DOMO_ACCESS_TOKEN. Save this as first_script.py:
import asyncio
import os
from dotenv import load_dotenv
from crew_dcs.auth import DomoTokenAuth
from crew_dcs.routes import dataset as dataset_routes
load_dotenv() # copies the lines in .env into environment variables
async def main():
async with DomoTokenAuth(
domo_instance=os.environ["DOMO_INSTANCE"],
domo_access_token=os.environ["DOMO_ACCESS_TOKEN"],
) as auth:
# 1. List a few DataSets.
res = await dataset_routes.search_datasets(auth=auth, maximum=3)
print("search ok:", res.is_success, "- status", res.status)
if not res.response:
print("No DataSets found for this user.")
return
for ds in res.response:
print(ds["databaseId"], ds["name"])
# 2. Fetch the first one by its id.
dataset_id = res.response[0]["databaseId"]
res = await dataset_routes.get_dataset_by_id(dataset_id=dataset_id, auth=auth)
print("fetch ok:", res.is_success)
print(res.response["name"], "-", res.response["rowCount"], "rows")
asyncio.run(main())
Run it with uv run python first_script.py from the project folder where you
installed crew-dcs. (In a virtual environment, activate it and run
python first_script.py.)
What you should see¶
The ids and names will be your own DataSets. Yours will look like this:
search ok: True - status 200
1a2b3c4d-0000-0000-0000-000000000001 Sales by region
1a2b3c4d-0000-0000-0000-000000000002 Customer list
1a2b3c4d-0000-0000-0000-000000000003 Web traffic
fetch ok: True
Sales by region - 5 rows
If it prints No DataSets found for this user., the user behind your token cannot
see any DataSets. Try again with a token for a user who can.
See the request
Routes accept debug_api=True, for example
search_datasets(auth=auth, maximum=3, debug_api=True). It logs the request and
response at the DEBUG level, with credentials masked. To see them, call
set_domolibrary_logger (from crew_dcs.utils.logging) before main() runs:
from crew_dcs.utils.logging import set_domolibrary_logger
set_domolibrary_logger(min_level="DEBUG")
They print when the async with block ends.
Check it in Domo¶
Open Data Center in Domo and find the first DataSet from your list, either by its name or by
opening https://mycompany.domo.com/datasources/<id>/details/overview with the id the script printed. The
name and row count on that page should match the last line of the output. (Menu names and
URLs can differ by Domo version.)
What just happened¶
Authentication. DomoTokenAuth holds your instance and access token. You hand
the same auth object to every call, so each call knows who you are. The
async with line closes the connection when the block ends.
Async. Every Domo call in crew-dcs is async. That means you write await
in front of it, inside an async def function, and start the function with
asyncio.run(main()). await just means "wait here for Domo to answer".
Routes. A route is one Python function for one Domo endpoint (one URL in Domo's API). Here:
search_datasetsis the DataSet search, like the search box in the Data Center.maximum=3caps it at three results.get_dataset_by_idfetches one DataSet's details.
Both live in crew_dcs.routes.dataset.
Reading the result. A route gives back one response object. Two things on it matter most:
.is_successisTruewhen Domo answered with a success status..responseholds what Domo sent back. Forsearch_datasetsthat is a list of DataSets, each a dictionary (a set of named values, likeds["name"]). Forget_dataset_by_idit is one dictionary.
These two DataSet routes raise an error when Domo answers with a failure, so
when the call returns, it worked. An id Domo reports as "Not Found" raises
DatasetNotFoundError; any other failure raises Dataset_GET_Error. You can
import both from crew_dcs.routes.dataset. Other routes may behave differently,
so check a route's reference page.
Next¶
For the bigger picture, read How crew-dcs is organised. If a call fails, see Errors.
Routes are the building blocks: one call, one Domo endpoint. Classes such as
DomoDataset wrap routes into objects you can ask for things, for example a
DataSet that knows how to fetch its own details. See the
DomoDataset reference and
the dataset routes reference.
Stuck?¶
Upgrade crew-dcs to the latest version first, because bugs are fixed often. If it still fails, ask in the DataCrew community Slack: datacrew.space/slack.