Run your own agent code in CI or a cron job with the Metorial SDK
For agent code built on the Metorial SDK: the job reads a secret API key from an environment variable and passes stored provider auth config IDs to its session.
When agent code you wrote with the Metorial SDK runs in CI, a cron job or a background worker, it authenticates with a secret API key from an environment variable and creates its own session. Nothing opens a browser at run time. For apps that need a user's consent, the user connects once in your app, and the job passes the stored provider auth config ID when it creates the session.
What you'll learn
- Which steps need a browser and which do not
- How to create a key for the job and find the stored connection
- How to make the job fail clearly when something is missing
Before you start
- Install the CLI and sign in
- A provider deployment for each app the agent uses.
metorial-admin providers deployments listshows them.
What needs a browser
| Step | Browser | When |
|---|---|---|
| Authenticate to Metorial | No | Every run, with the API key |
| Create a session and call tools | No | Every run |
| A user's OAuth consent for an app | Yes | Once, ahead of time, in your app |
| Shared credentials such as an API key or a service account | No | An admin sets them once |
The job cannot fix a missing connection. It should stop and say which app needs attention.
Set up the job
Create a key for the job
Give each job its own key, in the instance it runs against. Copy the secret from the output. It is shown only once.
Useful options:
--expires-at: sets an expiry date.--ip-filters: restricts the key to the addresses you list.
Find the stored connection
For an app that needs a sign-in, the job needs a provider auth config ID. Per-user IDs come from your database, where the setup flow stored them. For a shared service account, connect it once and look up its ID:
Providers that use an API key set on the deployment need no ID.
Store the values as secrets
Put the key in your CI system's secret store and expose it as METORIAL_API_KEY. In GitHub Actions:
Write the job
Install the packages, then save the file as job.ts or job.py. It exits with code 1 on any failure, so the run shows as failed.
Check it worked
The job log ends with the agent's final text and exit code 0. List the tool calls it made:
To see failures, run metorial-admin sessions errors list --limit 5.
Troubleshooting
Invalid API key
METORIAL_API_KEY is empty, truncated or from another instance. The TypeScript SDK prints [METORIAL ERROR]: unauthorized - Invalid API key, and the Python SDK prints Invalid API key status=401. Check that the secret is mapped into the job step.
API key is expired or has been revoked
The key reached its expiry or was revoked. Create a new one and update the CI secret. metorial-admin api-keys rotate replaces a key you still have access to.
This API key is not allowed from your IP address
The key has IP filters, and the runner's address is not among them. Hosted runners often use a different address on each run.
Provider requires an auth config to be provided
The session included a provider deployment that needs a sign-in, and the provider auth config ID was missing. Check that the user finished the setup flow and that the ID reached the job.
Next steps
- Let each user connect their own account: collect consent in your app and store the ID
- Limit the tools an agent can use: give the job only the tools it needs
- Organizations, projects, and environments: production and sandbox instances and their keys