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

# Events API examples

> Copy/paste code examples for sending events to Loops via the API, CLI, and SDKs to trigger workflows and attach event properties, with sample payloads.

## Send an event

[API reference](https://loops.so/docs/api-reference/send-event.md)\
[CLI reference](https://loops.so/docs/cli/events.md#send)

```bash CLI theme={"dark"}
loops events send testEvent --email test@example.com
```

```js JavaScript theme={"dark"}
await fetch("https://app.loops.so/api/v1/events/send", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <your-api-key>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    email: "test@example.com",
    eventName: "testEvent",
  }),
});
```

```js JavaScript SDK theme={"dark"}
import { LoopsClient } from "loops";

const loops = new LoopsClient("<your-api-key>");

const response = await loops.sendEvent({
  email: "test@example.com",
  eventName: "testEvent",
});
```

```php PHP SDK theme={"dark"}
use Loops\LoopsClient;

$loops = new LoopsClient("<your-api-key>");

$result = $loops->events->send(
  email: 'test@example.com',
  event_name: 'testEvent',
);
```

```ruby Ruby SDK theme={"dark"}
response = LoopsSdk::Events.send(
  email: "test@example.com",
  event_name: "testEvent",
)
```

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

response = requests.post(
    "https://app.loops.so/api/v1/events/send",
    headers={
        "Authorization": "Bearer <your-api-key>",
        "Content-Type": "application/json"
    },
    json={
        "email": "test@example.com",
        "eventName": "testEvent",
    }
)
```

## Send an event with event properties

Include data that can be used in your workflow emails triggered by the event.

[API reference](https://loops.so/docs/api-reference/send-event.md)\
[CLI reference](https://loops.so/docs/cli/events.md#send)

```bash CLI {3} theme={"dark"}
loops events send testEvent \
  --email test@example.com \
  --props ./event-props.json
```

```js JavaScript {10-12} theme={"dark"}
await fetch("https://app.loops.so/api/v1/events/send", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <your-api-key>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    email: "test@example.com",
    eventName: "testEvent",
    eventProperties: {
      "testProperty": "testValue",
    },
  }),
});
```

```js JavaScript SDK {8-10} theme={"dark"}
import { LoopsClient } from "loops";

const loops = new LoopsClient("<your-api-key>");

const response = await loops.sendEvent({
  email: "test@example.com",
  eventName: "testEvent",
  eventProperties: {
    "testProperty": "testValue",
  },
});
```

```php PHP SDK {8-10} theme={"dark"}
use Loops\LoopsClient;

$loops = new LoopsClient("<your-api-key>");

$result = $loops->events->send(
  email: 'test@example.com',
  event_name: 'testEvent',
  event_properties: [
    'testProperty' => 'testValue',
  ],
);
```

```ruby Ruby SDK {4-6} theme={"dark"}
response = LoopsSdk::Events.send(
  email: "test@example.com",
  event_name: "testEvent",
  event_properties: {
    testProperty: "testValue",
  },
)
```

```python Python {12-14} theme={"dark"}
import requests

response = requests.post(
    "https://app.loops.so/api/v1/events/send",
    headers={
        "Authorization": "Bearer <your-api-key>",
        "Content-Type": "application/json"
    },
    json={
        "email": "test@example.com",
        "eventName": "testEvent",
        "eventProperties": {
            "testProperty": "testValue",
        },
    }
)
```

## Send an event and update the contact

Include contact properties to update the contact as the event is sent.

[API reference](https://loops.so/docs/api-reference/send-event.md)\
[CLI reference](https://loops.so/docs/cli/events.md#send)

```bash CLI {3} theme={"dark"}
loops events send testEvent \
  --email test@example.com \
  --contact-props ./contact-props.json
```

```js JavaScript {10} theme={"dark"}
await fetch("https://app.loops.so/api/v1/events/send", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <your-api-key>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    email: "test@example.com",
    eventName: "testEvent",
    planName: "Pro", // Contact property
  }),
});
```

```js JavaScript SDK {8-10} theme={"dark"}
import { LoopsClient } from "loops";

const loops = new LoopsClient("<your-api-key>");

const response = await loops.sendEvent({
  email: "test@example.com",
  eventName: "testEvent",
  contactProperties: {
    planName: "Pro",
  },
});
```

```php PHP SDK {8-10} theme={"dark"}
use Loops\LoopsClient;

$loops = new LoopsClient("<your-api-key>");

$result = $loops->events->send(
  email: 'test@example.com',
  event_name: 'testEvent',
  contact_properties: [
    'planName' => 'Pro',
  ],
);
```

```ruby Ruby SDK {4-6} theme={"dark"}
response = LoopsSdk::Events.send(
  email: "test@example.com",
  event_name: "testEvent",
  contact_properties: {
    planName: "Pro",
  },
)
```

```python Python {12} theme={"dark"}
import requests

response = requests.post(
    "https://app.loops.so/api/v1/events/send",
    headers={
        "Authorization": "Bearer <your-api-key>",
        "Content-Type": "application/json"
    },
    json={
        "email": "test@example.com",
        "eventName": "testEvent",
        "planName": "Pro",  # Contact property
    }
)
```

## Send an event and subscribe to mailing lists

Include a `mailingLists` object to add the contact to one or more [mailing lists](https://loops.so/docs/contacts/mailing-lists.md). If the contact does not exist yet, it will be created and subscribed to the specified lists in the same request.

[API reference](https://loops.so/docs/api-reference/send-event.md)\
[CLI reference](https://loops.so/docs/cli/events.md#send)

```bash CLI {3} theme={"dark"}
loops events send testEvent \
  --email test@example.com \
  --list cm06f5v0e45nf0ml5754o9cix=true
```

```js JavaScript {10-12} theme={"dark"}
await fetch("https://app.loops.so/api/v1/events/send", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <your-api-key>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    email: "test@example.com",
    eventName: "testEvent",
    mailingLists: {
      "cm06f5v0e45nf0ml5754o9cix": true,
    },
  }),
});
```

```js JavaScript SDK {8-10} theme={"dark"}
import { LoopsClient } from "loops";

const loops = new LoopsClient("<your-api-key>");

const response = await loops.sendEvent({
  email: "test@example.com",
  eventName: "testEvent",
  mailingLists: {
    "cm06f5v0e45nf0ml5754o9cix": true,
  },
});
```

```php PHP SDK {8-10} theme={"dark"}
use Loops\LoopsClient;

$loops = new LoopsClient("<your-api-key>");

$result = $loops->events->send(
  email: 'test@example.com',
  event_name: 'testEvent',
  mailing_lists: [
    'cm06f5v0e45nf0ml5754o9cix' => true,
  ],
);
```

```ruby Ruby SDK {4-6} theme={"dark"}
response = LoopsSdk::Events.send(
  email: "test@example.com",
  event_name: "testEvent",
  mailing_lists: {
    "cm06f5v0e45nf0ml5754o9cix": true,
  },
)
```

```python Python {12-14} theme={"dark"}
import requests

response = requests.post(
    "https://app.loops.so/api/v1/events/send",
    headers={
        "Authorization": "Bearer <your-api-key>",
        "Content-Type": "application/json"
    },
    json={
        "email": "test@example.com",
        "eventName": "testEvent",
        "mailingLists": {
            "cm06f5v0e45nf0ml5754o9cix": True,
        },
    }
)
```

## Send an event with an idempotency key

Add an `Idempotency-Key` header to the request to prevent duplicate requests.

[API reference](https://loops.so/docs/api-reference/send-event.md#param-idempotency-key)\
[CLI reference](https://loops.so/docs/cli/events.md#send)

```bash CLI {3} theme={"dark"}
loops events send testEvent \
  --email test@example.com \
  --idempotency-key 550e8400-e29b-41d4-a716-446655440000
```

```js JavaScript {6} theme={"dark"}
await fetch("https://app.loops.so/api/v1/events/send", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <your-api-key>",
    "Content-Type": "application/json",
    "Idempotency-Key": "550e8400-e29b-41d4-a716-446655440000",
  },
  body: JSON.stringify({
    email: "test@example.com",
    eventName: "testEvent",
  }),
});
```

```js JavaScript SDK {8-10} theme={"dark"}
import { LoopsClient } from "loops";

const loops = new LoopsClient("<your-api-key>");

const response = await loops.sendEvent({
  email: "test@example.com",
  eventName: "testEvent",
  headers: {
    "Idempotency-Key": "550e8400-e29b-41d4-a716-446655440000",
  },
});
```

```php PHP SDK {8-10} theme={"dark"}
use Loops\LoopsClient;

$loops = new LoopsClient("<your-api-key>");

$result = $loops->events->send(
  email: 'test@example.com',
  event_name: 'testEvent',
  headers: [
    'Idempotency-Key' => '550e8400-e29b-41d4-a716-446655440000',
  ],
);
```

```ruby Ruby SDK {4-6} theme={"dark"}
response = LoopsSdk::Events.send(
  email: "test@example.com",
  event_name: "testEvent",
  headers: {
    "Idempotency-Key" => "550e8400-e29b-41d4-a716-446655440000",
  },
)
```

```python Python {8} theme={"dark"}
import requests

response = requests.post(
    "https://app.loops.so/api/v1/events/send",
    headers={
        "Authorization": "Bearer <your-api-key>",
        "Content-Type": "application/json",
        "Idempotency-Key": "550e8400-e29b-41d4-a716-446655440000",
    },
    json={
        "email": "test@example.com",
        "eventName": "testEvent",
    }
)
```


## Related topics

- [API Examples](https://loops.so/docs/api-reference/examples.md)
- [Workflows API examples](https://loops.so/docs/api-reference/examples/workflows.md)
- [Campaigns API examples](https://loops.so/docs/api-reference/examples/campaigns.md)
- [Contacts API examples](https://loops.so/docs/api-reference/examples/contacts.md)
- [Transactional email API examples](https://loops.so/docs/api-reference/examples/transactional-emails.md)
