# Using webhooks

## Create a subscription response

To create a subscription response, you will receive a webhook with a `201 Created` status, which includes a unique identifier and details about the newly created subscription. Use this data to build email notifications and integrate with Commerce APIs for additional information. Here is a sample of the response structure you can expect:

```json
{
  "id": "b5cd5cd4-7ae4-4a0c-b723-9b97bf42a9e8",
  "type": "subscription.created",
  "data": {
    "object": {
      "id": "23480199",
      "creationDate": "2021-05-13T09:38:05.000Z",
      "activationDate": "2021-05-13T05:00:00.000Z",
      "nextRenewalDate": "2021-07-13T05:00:00.000Z",
      // Additional subscription details...
    }
  }
}
```

Ensure to handle the `id` and `data` fields appropriately to extract the subscription details required for your application. This response can confirm the creation of a subscription and relay necessary information to the subscriber or internal systems for further processing.

In the example below, the initial `id` represents the subscription identifier. The subscription identifier appears as `subId` in the Subscription resource. For example, use `subId` to list all orders associated with a subscription in `https://www.digitalriver.com/v1/subscriptions/{subId}/orders`. It also provides additional subscription details.

```javascript
{
    "id": "b5cd5cd4-7ae4-4a0c-b723-9b97bf42a9e8",
    "type": "subscription.created",
    "data": {
        "object": {
            "id" : "23480199",
            "creationDate" : "2021-05-13T09:38:05.000Z",
            "activationDate" : "2021-05-13T05:00:00.000Z",
            "nextRenewalDate" : "2021-07-13T05:00:00.000Z",
            "nextBillingDate" : "2021-06-13T05:00:00.000Z",
            "expirationDate" : "2021-07-13T05:00:00.000Z",
            "graceDate" : "2021-06-20T05:00:00.000Z",
            "currentQuantity" : 1,
            "renewalQuantity" : 1,
            "autoRenewal" : true,
            "locale" : "en_US",
            "state" : "Subscribed",
            "duration" : 61,
            "frequency" : 31,
            "currentBillingCycleNumber" : 1,
            "totalNumberOfBillingCycle" : 2,
            "siteId" : "sub2test",
            "shopper" : {
                "id" : "20662635060199",
                "externalReferenceId" : "MCAGJAYW3JXJ"
            },
            "renewalPrice" : {
                "unitPrice" : 9.99,
                "locked" : true,
                "currency" : "USD"
            },
            "term" : {
                "termUnit" : "MONTHS",
                "termLength" : 1
            },
            "product" : {
                "id" : "5367865200",
                "displayName" : "APM_2_months_auto",
                "sku" : "SUBS_COMMITMENT"
            },
            "shipToAddress" : {
                "id" : "364164170199",
                "firstName" : "Subscription",
                "lastName" : "Automation",
                "companyName" : "Digital River",
                "line1" : "10380 Bren Rd W",
                "line2" : "Conjunto 304",
                "line3" : "Conjunto 304",
                "city" : "Minnetonka",
                "postalCode" : "55343",
                "countrySubdivision" : "MN",
                "country" : "US",
                "countryName" : "United States",
                "phoneNumber" : "952-253-1234",
                "emailAddress" : "subs_05132021043802AM707CSJQU@digitalriver.com",
                "countyName" : "Minnetonka"
            },
            "paymentOption" : {
                "nickName" : "VisaGJFV4P",
                "id" : "4033270199",
                "isDefault" : "true",
                "type" : "CreditCardMethod",
                "creditCard" : {
                    "expirationMonth" : "5",
                    "expirationYear" : "2023",
                    "displayableNumber" : "************1111",
                    "type" : "visa",
                    "displayName" : "Visa"
                },
                "address" : {
                    "id" : "364163290199",
                    "firstName" : "Subscription",
                    "lastName" : "Automation",
                    "companyName" : "DR",
                    "line1" : "10380 Bren Rd W",
                    "line2" : "Conjunto 304",
                    "line3" : "Conjunto 304",
                    "city" : "Minnetonka",
                    "postalCode" : "55343",
                    "countrySubdivision" : "MN",
                    "country" : "US",
                    "countryName" : "United States",
                    "phoneNumber" : "952-253-1234",
                    "emailAddress" : "subs_05132021043802AM707CSJQU@digitalriver.com",
                    "countyName" : "Minnetonka"
                }
            },
            "addOns" : [ {
                "product" : {
                    "id" : "5400082600",
                    "displayName" : "Subscription AddOn 1",
                    "sku" : "SUBS_ADDON"
                },
                "quantity" : 1,
                "renewalPrice" : {
                    "unitPrice" : 11.0,
                    "locked" : true,
                    "currency" : "USD"
                }
            } ]
        }
    }
}
```

## Retrieve an anonymous shopper's orders and production information

To retrieve an anonymous shopper's orders and product information, follow these steps:

{% stepper %}
{% step %}
**Obtain a token for the anonymous shopper**

Begin by obtaining an access token using the anonymous shopper's ID by sending a POST request to Digital River's OAuth token endpoint:

```markup
POST https://api.digitalriver.com/oauth20/token?grant_type=client_credentials&dr_userid={anonymousShopperId}
```

Replace `{anonymousShopperId}` with the actual shopper ID provided in the subscription details under `shopper.id`.
{% endstep %}

{% step %}
**Use the token to access orders and product Information**

Once you have received a session token, you can access the shopper's order and product information.

* **Get product information**:\
  Retrieve product details by making a GET request to:

  ```markup
  GET https://api.digitalriver.com/v1/shoppers/me/products/{productId}?expand=all
  ```

  Ensure to replace `{productId}` with the specific product ID you wish to view.
* **Get order information**:\
  Fetch order details by making a GET request to:

  ```markup
  GET https://api.digitalriver.com/v1/shoppers/me/orders?expand=all
  ```

  This allows you to list all orders associated with the anonymous shopper.
  {% endstep %}
  {% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.digitalriver.com/commerce-api/events/webhooks/using-webhooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
