Trigger runs from the CLI

@click.group()
def trigger():
    pass


@trigger.command()
@click.argument("task_id")
@click.argument("project_id")
@click.option("--api-key", envvar="TRIGGER_API_KEY", help="API key for authentication")
@click.option("--payload", help="Payload to include in the request")
def run(task_id: str, project_id: str, payload: str, api_key: str):
    """Run a Trigger task with the given task ID."""
    if not api_key:
        cli.print(
            "API key is required. Please provide it using --api-key option or set TRIGGER_API_KEY environment variable.",
            label="error",
        )
        return

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}",
    }
    data = {
        "payload": json.loads(payload) if payload else {},
    }

    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()
        run_id = response.json()["id"]
        run_url = f"https://cloud.trigger.dev/projects/v3/{project_id}/runs/{run_id}"
        cli.print("Task triggered successfully!", label="success")
        cli.print(f"View run at: {run_url}")
        if cli.confirm("Open run?"):
            subprocess.run(["open", run_url])
    except requests.exceptions.RequestException as e:
        cli.print(f"Error triggering task: {str(e)}", label="error")

I currently use the following script to trigger runs using the API from the CLI. Would be nice to be able to use the already implemented API functionality (i.e., trigger runs, cancel runs, list runs, list env vars, create schedules, etc.) directly from the Trigger.dev CLI.

Upvoters
Status

In Review

Board

πŸ’‘ Feature Request

Date

About 1 year ago

Author

Brian Le

Subscribe to post

Get notified by email when there are changes.