> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Automation

> Create a new engagement-triggered automation

export const PlansAvailable = ({plans = [], maxPackRequired}) => {
  let displayPlans = plans;
  if (plans && plans.length === 1) {
    const lowerCasePlan = plans[0].toLowerCase();
    if (lowerCasePlan === "business") {
      displayPlans = ["Launch", "Business", "Enterprise"];
    } else if (lowerCasePlan === "premium") {
      displayPlans = ["Premium", "Launch", "Business", "Enterprise"];
    }
  }
  return <Note>
Available on {displayPlans.length === 1 ? "the " : ""}
{displayPlans.join(", ").replace(/\b\w/g, l => l.toUpperCase())}{" "}
{displayPlans.length > 1 ? "plans" : "plan"}.

{maxPackRequired && <span onClick={() => window.open('https://www.ayrshare.com/docs/additional/maxpack', '_self')} className="flex items-center mt-2 cursor-pointer">
 <span className="px-1.5 py-0.5 rounded text-sm" style={{
    backgroundColor: '#C264B6',
    color: 'white',
    fontSize: '12px'
  }}>
   Max Pack required
 </span>
</span>}
</Note>;
};

export const HeaderAPI = ({noProfileKey, profileKeyRequired}) => <>
    <ParamField header="Authorization" type="string" required>
      <a href="/apis/overview#authorization">API Key</a> of the Primary Profile.
      <br />
      <br />
      Format: <code>Authorization: Bearer API_KEY</code>
    </ParamField>
    {!noProfileKey && (profileKeyRequired ? <ParamField header="Profile-Key" type="string" required>
          <a href="/apis/overview#profile-key-format">Profile Key</a> of a User Profile.
          <br />
          <br />
          Format: <code>Profile-Key: PROFILE_KEY</code>
        </ParamField> : <ParamField header="Profile-Key" type="string">
          <a href="/apis/overview#profile-key-format">Profile Key</a> of a User Profile.
          <br />
          <br />
          Format: <code>Profile-Key: PROFILE_KEY</code>
        </ParamField>)}
  </>;

<PlansAvailable plans={["business", "enterprise"]} maxPackRequired={false} />

<Note>
  **Beta.** See the [Automations Overview](/apis/automations/overview) for the full feature description, including the Beta status.
</Note>

Create a new automation that fires one or more actions when any of its triggers matches. The automation activates immediately. See the [Overview](/apis/automations/overview) for the full catalog of triggers, actions, template variables, and limits.

The connected Instagram account is derived server-side from the caller's linked profile — **do not pass `accountId`** in the body; the request will be rejected as a validation error if you do. Pass a `profileKey` header to operate under a child profile.

## Header Parameters

<HeaderAPI noProfileKey={false} />

## Body Parameters

<ParamField body="platform" type="string" required>
  Target platform. Only `instagram` is supported in v1.
</ParamField>

<ParamField body="name" type="string" required>
  Short human-readable name. Used in the dashboard and activity logs.
</ParamField>

<ParamField body="description" type="string">
  Optional longer description.
</ParamField>

<ParamField body="triggers" type="array" required>
  One or more triggers (1–50). Each entry is a discriminated union on `type`. See [Overview / Triggers](/apis/automations/overview#triggers) for the full catalog.

  ```json comment_keyword trigger theme={"system"}
  {
    "type": "comment_keyword",
    "postId": "17895695668004550",       // required — Instagram media id
    "keywords": ["INFO", "LINK"]         // required — ≥1 entry, case-insensitive
  }
  ```

  ```json story_reply trigger theme={"system"}
  {
    "type": "story_reply",
    "storyId": "17900000000000000"       // optional — scope to one story
  }
  ```

  ```json dm_reaction trigger theme={"system"}
  {
    "type": "dm_reaction",
    "emoji": "❤️"                         // optional — scope to one emoji
  }
  ```

  ```json dm_keyword trigger theme={"system"}
  {
    "type": "dm_keyword",
    "keywords": ["help", "support"]      // required — ≥1 entry, case-insensitive
  }
  ```
</ParamField>

<ParamField body="actions" type="array" required>
  One or more actions (1–50). Each entry is a discriminated union on `type`. See [Overview / Actions](/apis/automations/overview#actions) for the full catalog. Every action also accepts an optional top-level `dedupWindowMinutes` overriding the default 7-day per-recipient dedup window (`0` to disable, max `525600`).

  ```json send_dm action theme={"system"}
  {
    "type": "send_dm",
    "message": "Hey {{recipient_username}}, here is the link you wanted: https://example.com"
  }
  ```

  ```json fire_webhook action theme={"system"}
  {
    "type": "fire_webhook"
    // POSTs to your account-level webhook URL (configured in the dashboard).
    // See Overview for the JSON payload shape.
  }
  ```

  ```json send_email action theme={"system"}
  {
    "type": "send_email",
    "to": "alerts@example.com",
    "subject": "New {{platform}} engagement from @{{recipient_username}}",
    "message": "<p>{{recipient_username}} engaged with: {{comment_text}}</p>"
  }
  ```

  ```json Action with per-action dedup override theme={"system"}
  {
    "type": "send_dm",
    "message": "Thanks {{recipient_username}}!",
    "dedupWindowMinutes": 1440           // 24h instead of the 7-day default
  }
  ```
</ParamField>

<Note>
  **Platform compatibility.** Each trigger and action declares which platforms it supports. The validator rejects the request when an entry's `type` is not supported on the requested `platform`. All triggers and `send_dm` are currently Instagram-only; `fire_webhook` and `send_email` are platform-agnostic.
</Note>

<RequestExample>
  ```bash cURL theme={"system"}
  curl \
  -H "Authorization: Bearer API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "platform": "instagram",
    "name": "Spring promo",
    "triggers": [
      {
        "type": "comment_keyword",
        "postId": "17895695668004550",
        "keywords": ["INFO", "LINK"]
      }
    ],
    "actions": [
      {
        "type": "send_dm",
        "message": "Hey {{recipient_username}}, here is the link you wanted: https://example.com"
      }
    ]
  }' \
  -X POST https://api.ayrshare.com/api/automations
  ```

  ```javascript JavaScript theme={"system"}
  const API_KEY = "API_KEY";

  fetch("https://api.ayrshare.com/api/automations", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      platform: "instagram",
      name: "Spring promo",
      triggers: [
        {
          type: "comment_keyword",
          postId: "17895695668004550",
          keywords: ["INFO", "LINK"],
        },
      ],
      actions: [
        {
          type: "send_dm",
          message: "Hey {{recipient_username}}, here is the link you wanted: https://example.com",
        },
      ],
    }),
  })
    .then((res) => res.json())
    .then((json) => console.log(json))
    .catch(console.error);
  ```

  ```python Python theme={"system"}
  import requests

  payload = {
      "platform": "instagram",
      "name": "Spring promo",
      "triggers": [
          {
              "type": "comment_keyword",
              "postId": "17895695668004550",
              "keywords": ["INFO", "LINK"],
          }
      ],
      "actions": [
          {
              "type": "send_dm",
              "message": "Hey {{recipient_username}}, here is the link you wanted: https://example.com",
          }
      ],
  }

  headers = {
      "Authorization": "Bearer API_KEY",
      "Content-Type": "application/json",
  }

  r = requests.post(
      "https://api.ayrshare.com/api/automations",
      json=payload,
      headers=headers,
  )

  print(r.json())
  ```

  ```php PHP theme={"system"}
  $curl = curl_init();

  $data = [
      "platform" => "instagram",
      "name" => "Spring promo",
      "triggers" => [
          [
              "type" => "comment_keyword",
              "postId" => "17895695668004550",
              "keywords" => ["INFO", "LINK"],
          ],
      ],
      "actions" => [
          [
              "type" => "send_dm",
              "message" => "Hey {{recipient_username}}, here is the link you wanted: https://example.com",
          ],
      ],
  ];

  curl_setopt_array($curl, [
      CURLOPT_URL => "https://api.ayrshare.com/api/automations",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => json_encode($data),
      CURLOPT_HTTPHEADER => [
          "Authorization: Bearer API_KEY",
          "Content-Type: application/json",
      ],
  ]);

  $response = curl_exec($curl);
  curl_close($curl);
  echo $response;
  ```
</RequestExample>

<ResponseExample>
  ```json 200: Success theme={"system"}
  {
    "status": "success",
    "id": "auto_9xKp2Lm4nQ"
  }
  ```

  ```json 400: Validation failed — unknown template variable theme={"system"}
  {
    "action": "request",
    "status": "error",
    "code": 473,
    "message": "Validation failed. Please see: https://www.ayrshare.com/docs/introduction",
    "details": {
      "formErrors": [],
      "fieldErrors": {
        "actions": ["message contains unknown template variables"]
      }
    }
  }
  ```

  ```json 400: Validation failed — comment_keyword trigger missing keywords theme={"system"}
  {
    "action": "request",
    "status": "error",
    "code": 473,
    "message": "Validation failed. Please see: https://www.ayrshare.com/docs/introduction",
    "details": {
      "formErrors": [],
      "fieldErrors": {
        "triggers": ["Invalid input: expected array, received undefined"]
      }
    }
  }
  ```

  ```json 400: Validation failed — unrecognized field (e.g. accountId) theme={"system"}
  {
    "action": "request",
    "status": "error",
    "code": 473,
    "message": "Validation failed. Please see: https://www.ayrshare.com/docs/introduction",
    "details": {
      "formErrors": ["Unrecognized key: \"accountId\""],
      "fieldErrors": {}
    }
  }
  ```

  ```json 403: Plan tier required theme={"system"}
  {
    "action": "automation",
    "status": "error",
    "code": 468,
    "message": "Instagram DM automations require a Business or Enterprise plan"
  }
  ```

  ```json 403: Feature flag not yet enabled theme={"system"}
  {
    "action": "request",
    "status": "error",
    "code": 472,
    "message": "This feature is not yet available on your account. Please contact us if you'd like early access."
  }
  ```

  ```json 400: No linked social account for this platform theme={"system"}
  {
    "action": "automation",
    "status": "error",
    "code": 471,
    "message": "No social account linked on the requested platform for this profile. Link the account on the Social Accounts page and try again."
  }
  ```

  ```json 429: Active automation cap reached theme={"system"}
  {
    "action": "automation",
    "status": "error",
    "code": 470,
    "message": "Active automation limit reached for your plan tier"
  }
  ```
</ResponseExample>
