Code examples - AWS Marketplace

Code examples

The following code examples are provided to help you integrate your SaaS product with the AWS Marketplace APIs required for publishing and maintaining your product.

ResolveCustomer code example

The following code example is relevant for all pricing models. The Python example exchanges a x-amzn-marketplace-token token for a customerID. This code would run in an application on your registration website, when they are redirected there from the AWS Marketplace Management Portal. The redirect is a POST request that includes the token.

For more information on ResolveCustomer, see ResolveCustomer in the AWS Marketplace Metering Service API Reference.

# Import AWS Python SDK and urllib.parse import boto3 import urllib.parse as urlparse # Resolving Customer Registration Token formFields = urlparse.parse_qs(postBody) regToken = formFields['x-amzn-marketplace-token'] # If regToken present in POST request, exchange for customerID if (regToken): marketplaceClient = boto3.client('meteringmarketplace') customerData = marketplaceClient.resolve_customer(regToken) productCode = customerData['ProductCode'] customerID = customerData['CustomerIdentifier'] # TODO: Store customer information # TODO: Validate no other accounts share the same customerID

Example response

{ 'CustomerIdentifier': 'string', 'ProductCode': 'string' }

GetEntitlement code example

The following code example is relevant for SaaS products with the contract and SaaS contract with consumption pricing model. The Python example verifies that a customer has an active entitlement.

For more information on GetEntitlement, see GetEntitlement in the AWS Marketplace Entitlement Service API Reference.

# Import AWS Python SDK import boto3 marketplaceClient = boto3.client('marketplace-entitlement') # Filter entitlements for a specific customerID # # productCode is supplied after the AWS Marketplace Ops team has published # the product to limited # # customerID is obtained from the ResolveCustomer response entitlement = marketplaceClient.get_entitlements({ 'ProductCode': 'productCode', 'Filter' : { 'CUSTOMER_IDENTIFIER': [ 'customerID', ] }, 'NextToken' : 'string', 'MaxResults': 123 }) # TODO: Verify the dimension a customer is subscribed to and the quantity, # if applicable

Example response

The returned value will correspond with the dimensions created when you created the product in the AWS Marketplace Management Portal.

{ "Entitlements": [ { "CustomerIdentifier": "string", "Dimension": "string", "ExpirationDate": number, "ProductCode": "string", "Value": { "BooleanValue": boolean, "DoubleValue": number, "IntegerValue": number, "StringValue": "string" } } ], "NextToken": "string" }

BatchMeterUsage code example

The following code example is relevant for SaaS subscription and contract with consumption pricing models, but not for SaaS contract products without consumption. The Python example sends a metering record to AWS Marketplace to charge your customers for pay-as-you-go fees.

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # AWS Marketplace can only accept records for up to an hour in the past. # # productCode is supplied after the AWS Marketplace Ops team has # published the product to limited # # customerID is obtained from the ResolveCustomer response # Import AWS Python SDK import boto3 usageRecord = [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'customerID', 'Dimension': 'string', 'Quantity': 123 } ] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage(usageRecord, productCode)

For more information on BatchMeterUsage, see BatchMeterUsage in the AWS Marketplace Metering Service API Reference.

Example response

{ 'Results': [ { 'UsageRecord': { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 }, 'MeteringRecordId': 'string', 'Status': 'Success' | 'CustomerNotSubscribed' | 'DuplicateRecord' }, ], 'UnprocessedRecords': [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 } ] }