> ## 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 automation for an Instagram account or Facebook Page that fires one or more actions, such as send_dm, when any of its triggers matches.

> [!NOTE]
>
> **Beta.** See the [Automations Overview](https://www.ayrshare.com/docs/apis/automations/overview.md) for the full feature description, including the Beta status.

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

The connected Instagram account or Facebook Page 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

**Authorization** `string` *required*

[API Key](https://www.ayrshare.com/docs/apis/overview.md#authorization) of the Primary Profile.

Format: `Authorization: Bearer API_KEY`
**Profile-Key** `string`

[Profile Key](https://www.ayrshare.com/docs/apis/overview.md#profile-key-format) of a User Profile.

Format: `Profile-Key: PROFILE_KEY`

## Body Parameters

**platform** `string` *required*

Target platform: `instagram` or `facebook` (the linked Facebook Page). Facebook supports the `comment_keyword`, `dm_keyword` and `dm_reaction` triggers. See [Facebook Pages](https://www.ayrshare.com/docs/apis/automations/overview.md#facebook-pages).

**name** `string` *required*

Short human-readable name. Used in the dashboard and activity logs.

**description** `string`

Optional longer description.

**triggers** `array` *required*

One or more triggers (1–50). Each entry is a discriminated union on `type`. See [Overview / Triggers](https://www.ayrshare.com/docs/apis/automations/overview.md#triggers) for the full catalog.

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

```json comment_keyword trigger — every post (evergreen) theme={"system"}
{
  "type": "comment_keyword",
  "postId": "all",                     // fires on any post or reel on the account
  "keywords": ["INFO", "LINK"]
}
```

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

```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
}
```

**actions** `array` *required*

One or more actions (1–50). Each entry is a discriminated union on `type`. See [Overview / Actions](https://www.ayrshare.com/docs/apis/automations/overview.md#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
}
```

> [!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 support Instagram; `comment_keyword`, `dm_keyword` and `dm_reaction` also support Facebook. `send_dm` supports both, `fire_webhook` and `send_email` are platform-agnostic.

```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;
```

```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": "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"
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.