# Trigger a DataAgents workflow

Send a lead from your website to a DataAgents workflow. Your server submits the form fields to a custom trigger and receives a job ID. Use that ID to track the run while the workflow enriches the lead or sends it to your CRM.

## Try the example form<span class="example-pill" aria-hidden="true"></span>

<a href="/examples/data-agents/lead-form.html" target="_blank" rel="noopener">Try the sample lead form</a> with the prefilled fictional contact. Submitting it shows a local confirmation. The steps below connect your own form to a real workflow.

<img src="/images/data-agents/lead-form.png" alt="Northstar Systems demo request form filled in with a fictional lead" width="1680" height="896" />

## Connect a custom trigger

In the dashboard, go to **DataAgents → Workflows**. Open the menu on an event-based workflow and choose **Set trigger → Custom trigger → Create new trigger**. Name the trigger `web-lead` for this example and enable the workflow. Your API token and workflow need to belong to the same organization. [Dashboard setup for custom triggers](https://portal.snapaddy.com/knowledge/how-do-i-trigger-a-workflow-in-snapaddy-dataagents-via-the-api)

## Connect the form to your server

Use the same input names in your form and in the workflow payload. A plain HTML form can submit to your server without any browser JavaScript.

See the [API reference](/reference/data-agents/) for the full request schema.

```html
<form method="post" action="/api/leads">
  <label>First name <input name="firstName" autocomplete="given-name" required maxlength="100"></label>
  <label>Last name <input name="lastName" autocomplete="family-name" required maxlength="100"></label>
  <label>Work email <input name="email" type="email" autocomplete="email" required maxlength="254"></label>
  <button type="submit">Request demo</button>
</form>
```

Mount the following TypeScript handler at `POST /api/leads` in your Node.js server framework. It takes a standard `Request` and returns a `Response`. Set `SNAPADDY_API_KEY` to your [personal API token](/authentication/) in the server environment.

```typescript
export async function submitLead(request: Request): Promise<Response> {
  const form = await request.formData().catch(() => null);
  const readField = (name: string) => {
    const value = form?.get(name);
    return typeof value === "string" ? value.trim() : "";
  };
  const firstName = readField("firstName");
  const lastName = readField("lastName");
  const email = readField("email");

  if (!firstName || firstName.length > 100 ||
      !lastName || lastName.length > 100 ||
      email.length > 254 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
    return Response.json({ error: "Enter both names and a valid email." }, { status: 400 });
  }

  const apiKey = process.env.SNAPADDY_API_KEY;
  if (!apiKey) {
    return Response.json({ error: "The lead form is unavailable." }, { status: 503 });
  }

  try {
    const upstream = await fetch("https://backend.snapaddy.com/job-management/jobs/workflow", {
      method: "POST",
      headers: { "Content-Type": "application/json", "x-api-key": apiKey },
      body: JSON.stringify({ trigger: "web-lead", data: { firstName, lastName, email } }),
      signal: AbortSignal.timeout(10_000)
    });

    if (upstream.status === 201) {
      const { id } = await upstream.json();
      if (typeof id === "string" && id) {
        return Response.json({ jobId: id }, { status: 202 });
      }
    }
  } catch {
    // A connection failure or timeout can leave the job outcome unknown.
  }

  return Response.json({ error: "The request could not be confirmed." }, { status: 502 });
}
```

Your website can show a confirmation when the handler returns `202`. A plain HTML form will display the JSON response as written. To show an inline message like the example, submit to your own handler with `fetch` and display success only on `202`.

<img src="/images/data-agents/lead-form-confirmation.png" alt="The sample form showing a simulated submission confirmation" width="1680" height="1182" />

## Test the workflow directly

To check the trigger independently of the form, send the same payload from your terminal. Use a test workflow if its steps write to a CRM or another connected system.

See the [API reference](/reference/data-agents/) for the full request schema.

```bash
curl --fail-with-body \
  'https://backend.snapaddy.com/job-management/jobs/workflow' \
  -H "x-api-key: $SNAPADDY_API_KEY" \
  -H 'Content-Type: application/json' \
  --data '{
    "trigger": "web-lead",
    "data": {
      "firstName": "Alex",
      "lastName": "Morgan",
      "email": "alex@example.com"
    }
  }'
```

```typescript
const apiKey = process.env.SNAPADDY_API_KEY;
if (!apiKey) throw new Error("Set SNAPADDY_API_KEY");

const payload = {
  trigger: "web-lead",
  data: {
    firstName: "Alex",
    lastName: "Morgan",
    email: "alex@example.com"
  }
};
const response = await fetch("https://backend.snapaddy.com/job-management/jobs/workflow", {
  method: "POST",
  headers: { "x-api-key": apiKey, "Content-Type": "application/json" },
  body: JSON.stringify(payload),
});
if (!response.ok) throw new Error(`Request failed (${response.status})`);
console.log(await response.json());
```

```python
import json
import os
from urllib.request import Request, urlopen

payload = {
    "trigger": "web-lead",
    "data": {
        "firstName": "Alex",
        "lastName": "Morgan",
        "email": "alex@example.com"
    }
}
request = Request(
    "https://backend.snapaddy.com/job-management/jobs/workflow",
    method="POST",
    headers={"x-api-key": os.environ["SNAPADDY_API_KEY"], "Content-Type": "application/json"},
    data=json.dumps(payload).encode(),
)
with urlopen(request, timeout=20) as response:
    print(json.load(response))
```

A `201` response includes the workflow job's `id`. Save it so you can [track the job](/platform/jobs/) through completion. Each successful request creates a job. Disable repeat clicks while a submission is pending, and use a submission ID in your application to prevent duplicate jobs. A timeout can leave the result unknown, so avoid automatically replaying the same request. If the API returns `404`, check the trigger name and organization. If it returns `422`, check that the workflow is enabled.

After submitting a lead, [read the workflow results](/data-agents/results/) to see whether the run succeeded and deliver its output to your application.

[DataAgents Workflow Runs API reference](/reference/data-agents/)

## Process a batch of leads

When you already have a set of submissions, send their data as an array. Every array entry starts its own workflow job. A group name helps an operator recognize the batch when reviewing progress.

See the [API reference](/reference/data-agents/) for the full request schema.

```bash
curl --fail-with-body 'https://backend.snapaddy.com/job-management/jobs/workflow/bulk' \
  -H "x-api-key: $SNAPADDY_API_KEY" -H 'Content-Type: application/json' \
  --data '{
    "trigger":"web-lead",
    "groupName":"Partner event leads",
    "data":[
      {"firstName":"Alex","lastName":"Morgan","email":"alex@example.com"},
      {"firstName":"Sam","lastName":"Lee","email":"sam@example.com"}
    ]
  }'
```

```typescript
const apiKey = process.env.SNAPADDY_API_KEY;
if (!apiKey) throw new Error("Set SNAPADDY_API_KEY");

const payload = {
  trigger: "web-lead",
  groupName: "Partner event leads",
  data: [
    {
      firstName: "Alex",
      lastName: "Morgan",
      email: "alex@example.com"
    },
    {
      firstName: "Sam",
      lastName: "Lee",
      email: "sam@example.com"
    }
  ]
};
const response = await fetch("https://backend.snapaddy.com/job-management/jobs/workflow/bulk", {
  method: "POST",
  headers: { "x-api-key": apiKey, "Content-Type": "application/json" },
  body: JSON.stringify(payload),
});
if (!response.ok) throw new Error(`Request failed (${response.status})`);
console.log(response.status);
```

```python
import json
import os
from urllib.request import Request, urlopen

payload = {
    "trigger": "web-lead",
    "groupName": "Partner event leads",
    "data": [
        {
            "firstName": "Alex",
            "lastName": "Morgan",
            "email": "alex@example.com"
        },
        {
            "firstName": "Sam",
            "lastName": "Lee",
            "email": "sam@example.com"
        }
    ]
}
request = Request(
    "https://backend.snapaddy.com/job-management/jobs/workflow/bulk",
    method="POST",
    headers={"x-api-key": os.environ["SNAPADDY_API_KEY"], "Content-Type": "application/json"},
    data=json.dumps(payload).encode(),
)
with urlopen(request, timeout=20) as response:
    print(response.status)
```

A successful bulk request returns `201` with an empty body. Find the group using [Job Management](/platform/jobs/) to inspect progress or request cancellation.

## Find recent workflow runs

Use execution search to check which submissions triggered a workflow run. The search covers all workflow runs in your organization and requires an account with the **DataAgents Manager** role. Start with a small page, sorted by newest first. If you have a group ID, add `"filters":{"jobGroupIds":["YOUR_GROUP_ID"]}` to the body.

```bash
curl --fail-with-body 'https://backend.snapaddy.com/job-management/v2/jobs/workflow/search' \
  -H "x-api-key: $SNAPADDY_API_KEY" -H 'Content-Type: application/json' \
  --data '{"limit":10,"offset":0,"sortDirection":"DESC"}'
```

```typescript
const apiKey = process.env.SNAPADDY_API_KEY;
if (!apiKey) throw new Error("Set SNAPADDY_API_KEY");

const payload = {
  limit: 10,
  offset: 0,
  sortDirection: "DESC"
};
const response = await fetch("https://backend.snapaddy.com/job-management/v2/jobs/workflow/search", {
  method: "POST",
  headers: { "x-api-key": apiKey, "Content-Type": "application/json" },
  body: JSON.stringify(payload),
});
if (!response.ok) throw new Error(`Request failed (${response.status})`);
console.log(await response.json());
```

```python
import json
import os
from urllib.request import Request, urlopen

payload = {
    "limit": 10,
    "offset": 0,
    "sortDirection": "DESC"
}
request = Request(
    "https://backend.snapaddy.com/job-management/v2/jobs/workflow/search",
    method="POST",
    headers={"x-api-key": os.environ["SNAPADDY_API_KEY"], "Content-Type": "application/json"},
    data=json.dumps(payload).encode(),
)
with urlopen(request, timeout=20) as response:
    print(json.load(response))
```

Read the `results` array. To fetch subsequent pages, keep the filters and sort direction and increase `offset` by your page size. The maximum `limit` is 100.

To design the workflow itself, use the node documentation to choose its trigger, map incoming fields, and configure each action. [DataAgents workflow documentation](https://workflow-docs.snapaddy.com/) · [DataAgents API reference](/reference/data-agents/)
