# Create Subscription
source: https://developer.mastercard.com/commercial-event-notifications/documentation/use_cases/create_subscription/index.md

The API requires at least one [subscription](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md) to generate [notifications](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/notifications/notifications/index.md).
When creating a subscription, you provide a subject type and a filter [specification](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#specifications-array-required)
to set the scope of the notifications that you will receive.

You can create a subscription:

* With an empty filter specification, to receive all notifications that you are entitled to  
  OR
* With a filter specification populated with criteria, to receive notifications only for the conditions you have specified.

The filter is constructed using [field mappings](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/fieldmappings/index.md), which are the names of the fields you are constraining in the notification,
and the conditions that those fields must meet to receive the notification.
For example, you can specify a list of real card aliases, to receive notifications for ICCP authorizations that are funded by the corresponding real cards.

### Subscription With an Empty Specification {#subscription-with-an-empty-specification}

Diagram create-subscription

Create a subscription with an empty filter specification using the following call.

```java
//Calling Post subscription API.
//creating subscription resource to post.
Subscription subscriptionRequest = new Subscription();
subscriptionRequest.name("MySubscription")
        .subjectType(Subscription.SubjectTypeEnum.PAYMENT_AUTHORIZATION)
        .active(true);
SubscriptionApi subscriptionApi = new SubscriptionApi(client); //Passing the initialised API client to Subscription API
//calling a post to create subscription
 Subscription subscriptionResponse = null;
try {
    subscriptionResponse = subscriptionApi.subscriptionsPost(subscriptionRequest);
} catch (ApiException e) {
    System.err.println("Exception when calling Post SubscriptionAPI");
    e.printStackTrace();
}
System.out.println("Created subscription response:" + subscriptionResponse.toString());
```

In the above code:

* You are passing the initialized API client for authentication.
* Creating a subscription with a null specification by passing values for the parameters [name](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#name-string-required) and [subjectType](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#subjecttype-string-required), and by setting the subscription state to [active](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#active-boolean-required).

### Create Subscription With Filter Specification {#create-subscription-with-filter-specification}

Once you have determined the available [filter criteria](https://developer.mastercard.com/commercial-event-notifications/documentation/use_cases/determine_fields/index.md), you can create a subscription and define filter conditions
by including a [specification](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#specifications-array-required) comprising the fields and expected values.

```java

//Calling Post subscription API.
//creating subscription resource to post.
Subscription subscriptionRequest = new Subscription(); //Subscription
Specification specificationRequest = new Specification(); //Specification
FieldContent fieldContent = new FieldContent(); //FieldContent
fieldContent.contentType(TEXT).setValue("RcnAliasText");
specificationRequest
        .type(FIELD)
        .operator(WHERE)
        .children(new ArrayList<>())
        .fieldMappingName("purchaseRequest.rcnAlias") //FieldMapping
        .fieldOperator(EQUALS) //Filed Operator
        .expectedContent(fieldContent);
SubscriptionApi subscriptionApi = new SubscriptionApi(client);
//calling a post to create subscription
 Subscription subscriptionResponse = null;
try {
    subscriptionResponse = subscriptionApi.subscriptionsPost(subscriptionRequest);
} catch (ApiException e) {
    System.err.println("Exception when calling Post SubscriptionAPI");
    e.printStackTrace();
}
System.out.println("Created subscription response:" + subscriptionResponse.toString())
```

In the above code:

* You are passing the initialized API client for authentication
* You create a subscription with a filter specification by populating a [specification](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#specifications-array-required) structure `specificationRequest`, with the conditions that must be met for a notification to be generated

#### Specification structure {#specification-structure}

The specification structure `specificationRequest` defines the criteria to be checked by specifying:

* the entity being checked, which currently must be a FIELD
* a conditional operator, for example: WHERE for the first condition; AND or OR for subsequent conditions
* a child specification, which may be used to form complex criteria, but in this case is an empty array
* the fieldMappingName that identifies a field in the event - in the above example the RCN Alias
* a logical fieldOperator, used to check the field value, for example: EQUALS
* the [expectedContent](https://developer.mastercard.com/commercial-event-notifications/documentation/parameters/subscriptions/index.md#expectedcontent-object-required), against which the field will be checked, in this case "RcnAliasText"

### Try it Out {#try-it-out}


API Reference: `POST /subscriptions`

### Additional Information {#additional-information}

Refer to the [Codes and Formats](https://developer.mastercard.com/commercial-event-notifications/documentation/codeandformats/index.md) for more information about error codes.
