# Events

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


Whenever any important changes happen in your Chargebee site, they are recorded as Events. Event contains data about affected resources and additional details such as when it occurred.

You could listen to the events in your application by configuring Webhook url and handle it based on the type of event. We post the event data as request body with content type set as **application/json** to your webhook url.

For example, when a subscription is cancelled due to non payment, an event **"subscription\_cancelled"** is recorded and fired to your webhook url.

To mark a webhook notification successful, we expect the [HTTP status code](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes) to be 200 from your webhook url. If we don't receive 200 response code, we retry calling your webhook with exponential time interval for next 2 days. You could also try resending it from our admin console.

**API Version:** Chargebee supports multiple API versions now. The _api\_version_ attribute indicates the API version based on which the event _content_ is structured. While processing webhooks, ensure the _api\_version_ is same as the API version used by your webhook server's client library.

#### Securing your Webhook URL:[](#securing-your-webhook-url)

You can have **basic authentication** for the webhook url.

-   In the Webhook Settings page (_Settings > Webhooks_), check the option _My webhook URL is protected by basic authentication_.
-   Enter _Username_ and _Password_ and click Update Webhook URL OR Generate a random key and have it as part of your webhook url eg. http://yourapp.com/chargebee-webhook/cuktqaem0i2fkd5jt9cdtojcn9cvb3Y

In addition to securing your webhook, you can always fetch the event from Chargebee using [Retrieve an Event](/docs/api/v1/events/retrieve-an-event) API call.

Ensure that you keep the secrets secret :)

## Sample Event

```json
{
  "event": {
    "api_version": "v1",
    "content": {
      "customer": {
        "account_credits": 0,
        "allow_direct_debit": false,
        "auto_collection": "on",
        "card_status": "no_card",
        "created_at": 1517506759,
        "excess_payments": 0,
        "id": "__test__5SK0bLNFRFuCIi8Nj",
        "object": "customer",
        "refundable_credits": 0,
        "taxability": "taxable"
      },
      "subscription": {
        "created_at": 1517506759,
        "due_invoices_count": 0,
        "has_scheduled_changes": false,
        "id": "__test__5SK0bLNFRFuCIi8Nj",
        "object": "subscription",
        "plan_id": "plan1",
        "plan_quantity": 1,
        "started_at": 1517506759,
        "status": "in_trial",
        "trial_end": 1518802759,
        "trial_start": 1517506759
      }
    },
    "event_type": "subscription_created",
    "id": "ev___test__5SK0bLNFRFuCIipNm",
    "object": "event",
    "occurred_at": 1517506759,
    "source": "api",
    "user": "full_access_key_v1",
    "webhook_status": "scheduled"
  }
}
```

## Events attributes

## Input Parameters

- `id` (required, string, max chars=40)
  Uniquely identifies a event

- `occurred_at` (required, timestamp(UTC) in seconds)
  Timestamp indicating when this event had occurred.

- `source` (required, enumerated string, default=none)
  Source of the event
  Possible enum values:
    - `admin_console`
      Operation made through the Chargebee admin UI
    - `api`
      Operation made through the API
    - `scheduled_job`
      Operation made through the Scheduled Jobs
    - `hosted_page`
      Operation made through the Hosted Pages
    - `portal`
      Operation made through [Self-Serve Portal](https://www.chargebee.com/docs/2.0/inapp-self-serve-portal.html)
    - `system`
      Operation that are triggered by ChargeBee System
    - `none`
      If no source can be identified for an operation
    - `js_api`
      Operation made through the JS API
    - `migration`
      Deprecated
    - `bulk_operation`
      Operation that are triggerd through bulk operation.

- `user` (optional, string, max chars=150)
  The "user" that triggered the event. The value depends on the `source` attribute:
  
  -   When `source` is `admin_console`: the email address of the user that triggered the event.
  -   When `source` is `api`, `js_api` or `bulk_operation`: the [name](https://www.chargebee.com/docs/api_keys.html#create-an-api-key) of the API key that was used to trigger the event.
  -   When the `source` is `external_service`: the name of the service that called our webhook. Eg. `ADYEN`, `STRIPE`, `AMAZON_PAYMENTS` etc.
  -   When the `source` is `hosted_page` or `portal`: the `user` attribute is not passed.

- `event_type` (optional, enumerated string)
  The types of event provided by chargebee. Refer [event types](/docs/api/v1/events/event-types) for all the event types provided by us currently.
  Possible enum values:
    - `customer_created`
      Sent when a customer is created. This event happens when only a new customer is created or when a customer is automatically created during new subscription creation.
    - `customer_changed`
      Sent when a customer is changed
    - `customer_deleted`
      Sent when a customer is deleted
    - `subscription_created`
      Sent when a new subscription is created.
    - `subscription_started`
      Sent when a 'future' subscription gets started at the scheduled date.
    - `subscription_trial_ending`
      Sent when the customer's trial period is about to end.
    - `subscription_activated`
      Sent after the subscription has been moved from trial to active state
    - `subscription_changed`
      Sent after the subscription's recurring items have been changed
    - `subscription_trial_extended`
      Trial Extension
    - `subscription_cancellation_scheduled`
      Sent when subscription is scheduled to cancel at end of current term
    - `subscription_cancelling`
      Sent when the customer's subscription is nearing it's scheduled cancellation date.
    - `subscription_cancelled`
      Sent when the subscription gets cancelled. If cancelled due to non payment or card not present, the subscription will have the possible reason as 'cancel\_reason'.
    - `subscription_reactivated`
      Sent when the subscription is moved from cancelled state to active or in\_trial state
    - `subscription_renewed`
      Sent when the subscription is renewed from the current term.
    - `subscription_scheduled_cancellation_removed`
      Sent when scheduled cancellation is removed for the subscription.
    - `subscription_shipping_address_updated`
      Triggered when shipping address is added or updated for a subscription.
    - `subscription_deleted`
      Sent when a subscription has been deleted
    - `invoice_created`
      Event triggered (in the case of metered billing) when a "Pending" invoice is created that has usage related charges or line items to be added, before being closed. This is triggered only when the "Notify for Pending Invoices" option is enabled.
    - `invoice_generated`
      Event triggered when a new invoice is generated. In case of metered billing, this event is triggered when a "Pending" invoice is closed.
    - `invoice_updated`
      Triggered when changes are made to a finalized invoice, including voiding, deletion, invoice address updates, status changes, and payment changes such as applying or removing a payment, applying or removing a credit, and credit note creation. pending\_invoice\_updated is triggered for changes specific to pending invoices; invoice\_updated covers all other invoice changes.
    - `invoice_deleted`
      Event triggered when an invoice is deleted.
    - `subscription_renewal_reminder`
      Sent before each subscription's renewal based on plan's period
    - `transaction_created`
      Triggered when a transaction is recorded
    - `transaction_updated`
      Triggered when a transaction is updated. E.g. (1) When a transaction is removed, (2) or when an excess payment is applied on an invoice, (3) or when amount\_capturable gets updated.
    - `transaction_deleted`
      Triggered when a transaction is deleted.
    - `payment_succeeded`
      Sent when the payment is successfully collected
    - `payment_failed`
      Sent when attempt to charge customer's credit card fails
    - `payment_refunded`
      Sent when a payment refund is made
    - `payment_initiated`
      Sent when a payment is initiated via direct debit
    - `refund_initiated`
      Sent when a refund is initiated via direct debit
    - `card_added`
      Sent when a card is added for a customer.
    - `card_updated`
      Sent when the card is updated for a customer.
    - `card_expiring`
      Sent when the customer's credit card is expiring soon. Sent 30 days before the expiry date.
    - `card_expired`
      Sent when a card for a customer is expired
    - `card_deleted`
      Sent when a card is deleted for a customer

- `api_version` (optional, enumerated string, default=v1)
  The Chargebee API Version used for rendering this event content. While processing webhooks, ensure this version is same as the API version used by your webhook server's client library.
  Possible enum values:
    - `v1`
      Chargebee API version V1
    - `v2`
      Chargebee API version V2

- `content` (required, jsonobject)
  The JSON data associated with this event. Has resources (_subscription_ , _invoice_ etc) based on the [event type](/docs/api/v1/events/event-types). These resources are structured based on the Chargebee API version indicated by the _api\_version_ attribute.

- `webhooks` (optional, list of webhook)
  Array of webhook call statuses: one for each of the webhooks configured for the site. This object is only available after the first webhook call for the event has completed or timed out. Also, creation/updation of the `webhook` object data is a queued operation and hence there can be an additional delay of up to 5 seconds.
  - `id` (required, string, max chars=40)
    Uniquely identifies a webhook
  - `webhook_status` (required, enumerated string)
    -   **When the event resource is retrieved via API:** Represents the status of the webhook call made to this webhook.
    -   **When the event resource is passed as part of a webhook call:** The `webhooks` object is unavailable on the first webhook call for the event. For subsequent calls, this attribute holds the status from after the last retry.
    Possible enum values:
      - `not_configured`
        Webhook was not configured when this event occurred
      - `scheduled`
        Webhook call has been scheduled.
      - `succeeded`
        Webhook call was successful.
      - `re_scheduled`
        Webhook call has been rescheduled due failure(s) in previous call(s)
      - `failed`
        Webhook call has been suspended after the all retries have resulted in failure.
      - `skipped`
        Skipped as specified in request
      - `not_applicable`
        Webhook call is not applicable for this event.
      - `disabled`
        Disabled as it is no longer used.
      - `rate_limited`
        Webhook call was rate limited.

