View a markdown version of this page

Esempi di codice per l'integrazione di prodotti SaaS - Marketplace AWS

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Esempi di codice per l'integrazione di prodotti SaaS

Puoi utilizzare i seguenti esempi di codice per integrare il tuo prodotto SaaS (Software as a Service) con le Marketplace AWS API necessarie per la pubblicazione e la manutenzione del prodotto. Per ulteriori informazioni, consultare le sezioni indicate di seguito.

ResolveCustomeresempio di codice

Il seguente esempio di codice è rilevante per tutti i modelli di prezzo. L'esempio Python scambia un x-amzn-marketplace-token token con unCustomerIdentifier, ProductCodeLicenseArn, e. CustomerAWSAccountId CustomerAWSAccountIdè l' Account AWS ID associato all'abbonamento ed LicenseArn è un identificatore univoco per una licenza specifica concessa. Vengono utilizzati per il software acquistato tramite Marketplace AWS. Questo codice viene eseguito in un'applicazione sul sito Web di registrazione, quando si viene reindirizzati lì da. Portale di gestione Marketplace AWS Il reindirizzamento è una richiesta POST che include il token.

Per ulteriori informazioni ResolveCustomerin meritoResolveCustomer, consulta la sezione Marketplace AWS Metering Service API Reference.

Importante

Per le nuove integrazioni di prodotti SaaS, il CustomerIdentifier campo non è compilato nella risposta dell'API. ResolveCustomer Utilizza CustomerAWSAccountId e LicenseArn invece per l'identificazione del cliente.

# 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'][0] # If regToken present in POST request, exchange for customerID if (regToken): marketplaceClient = boto3.client('meteringmarketplace') customerData = marketplaceClient.resolve_customer(RegistrationToken=regToken) productCode = customerData['ProductCode'] customerID = customerData['CustomerIdentifier'] customerAWSAccountId = customerData['CustomerAWSAccountId'] licenseARN = customerData['LicenseArn'] # TODO: Store customer information # TODO: Validate no other accounts share the same customerID

Esempio di risposta

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

GetEntitlementesempio di codice

Il seguente esempio di codice è rilevante per i prodotti SaaS con contratto e contratto SaaS con modello di prezzo al consumo. L'esempio Python verifica che un cliente disponga di un'autorizzazione attiva.

Per ulteriori informazioni in meritoGetEntitlement, consulta l'articolo Marketplace AWS Entitlement GetEntitlementService API Reference.

# Import AWS Python SDK import boto3 marketplaceClient = boto3.client('marketplace-entitlement', region_name='us-east-1') # Filter entitlements for a specific customerID # # productCode is supplied after the Marketplace AWS Ops team has published # the product to limited # # customerID is obtained from the ResolveCustomer response entitlement = marketplaceClient.get_entitlements({ 'ProductCode': 'productCode', 'Filter' : { # Option 1: Using CustomerAWSAccountId (preferred) 'CUSTOMER_AWS_ACCOUNT_ID': [ 'awsAccountId', ] # Option 2: Using LICENSE_ARN (to get entitlements for the license) # 'LICENSE_ARN': [ # 'licenseARN', # ] # Option 3: Using CustomerIdentifier (existing integrations only, not supported for new integrations) # 'CUSTOMER_IDENTIFIER': [ # 'customerID', # ] }, 'NextToken' : 'string', 'MaxResults': 123 }) # TODO: Verify the dimension a customer is subscribed to and the quantity, # if applicable

Esempio di risposta

Il valore restituito corrisponde alle dimensioni create quando è stato creato il prodotto in. Portale di gestione Marketplace AWS

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

BatchMeterUsageesempio di codice

Il seguente esempio di codice è rilevante per i modelli di abbonamento e contratto SaaS con prezzi al consumo, ma non per i prodotti contrattuali SaaS senza consumo. L'esempio di Python invia un record di misurazione per addebitare Marketplace AWS ai clienti le tariffe con pagamento in base al consumo.

Importante

BatchMeterUsagenon supporta nuove CustomerIdentifier integrazioni di prodotti SaaS. Per nuove integrazioni, vedi. BatchMeterUsageesempio di codice: Con licenza ARN

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

Per ulteriori informazioni BatchMeterUsagein meritoBatchMeterUsage, consulta la sezione Marketplace AWS Metering Service API Reference.

Esempio di risposta

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

BatchMeterUsageesempio di codice: Con licenza ARN

Il seguente esempio di codice è rilevante per i prodotti SaaS che supportano gli accordi concorrenti. I LicenseArn e CustomerAWSAccountId vengono restituiti dall'ResolveCustomerAPI quando un acquirente si registra al tuo prodotto.

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # Marketplace AWS can only accept records for up to an hour in the past. # # LicenseArn and CustomerAWSAccountId are returned by the ResolveCustomer # API when a buyer registers to your product # Import AWS Python SDK import boto3 from datetime import datetime usageRecord = [{ 'LicenseArn' : 'licenseArn', 'Timestamp': datetime(2015, 1, 1), 'CustomerAWSAccountId': 'awsAccountId', 'Dimension': 'string', 'Quantity': 123 }] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage( UsageRecords = usageRecord )

Esempio di risposta

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

BatchMeterUsagecon un esempio di codice di etichettatura per l'allocazione dell'utilizzo (opzionale)

Il seguente esempio di codice è rilevante per gli abbonamenti e i contratti SaaS con modelli di prezzo di utilizzo, ma non per i prodotti contrattuali SaaS senza utilizzo. L'esempio di Python invia un record di misurazione con tag di allocazione dell'utilizzo appropriati per addebitare ai clienti le tariffe con pagamento Marketplace AWS in base al consumo.

Questo esempio utilizza LicenseArn eCustomerAWSAccountId, che vengono restituiti dall'API quando un acquirente si registra al ResolveCustomer tuo prodotto. Using LicenseArn supporta prodotti con Concurrent Agreements, in base ai quali un singolo acquirente può avere più contratti attivi per lo stesso prodotto.

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # Marketplace AWS can only accept records for up to an hour in the past. # # LicenseArn and CustomerAWSAccountId are returned by the # ResolveCustomer API when a buyer registers to your product. # Import AWS Python SDK import boto3 from datetime import datetime, timezone usageRecords = [ { "Timestamp": datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0), "CustomerAWSAccountId": "111122223333", "Dimension": "Dimension1", "LicenseArn": "arn:aws:license-manager::123456789012:license:l-exampleLicense12345", "Quantity": 10000, "UsageAllocations": [ { "AllocatedUsageQuantity": 5000, "Tags": [ {"Key": "user", "Value": "john"}, {"Key": "feature", "Value": "data-processing"}, {"Key": "job-name", "Value": "job11111"}, ] }, { "AllocatedUsageQuantity": 2500, "Tags": [ {"Key": "user", "Value": "john"}, {"Key": "feature", "Value": "data-storage"}, {"Key": "cluster-name", "Value": "cluster11111"}, ] }, { "AllocatedUsageQuantity": 2500, "Tags": [ {"Key": "user", "Value": "jane"}, {"Key": "feature", "Value": "data-storage"}, {"Key": "cluster-name", "Value": "cluster22222"}, ] }, ] }, { "Timestamp": datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0), "CustomerAWSAccountId": "111122223333", "Dimension": "Dimension2", "LicenseArn": "arn:aws:license-manager::123456789012:license:l-exampleLicense12345", "Quantity": 2000, "UsageAllocations": [ { "AllocatedUsageQuantity": 1000, "Tags": [ {"Key": "user", "Value": "john"}, {"Key": "feature", "Value": "data-processing"}, {"Key": "job-name", "Value": "job11111"}, ] }, { "AllocatedUsageQuantity": 1000, "Tags": [ {"Key": "user", "Value": "jane"}, {"Key": "feature", "Value": "data-processing"}, {"Key": "job-name", "Value": "job22222"}, ] }, ] } ] marketplaceClient = boto3.client('meteringmarketplace', region_name='us-east-1') response = marketplaceClient.batch_meter_usage( UsageRecords=usageRecords )

Per ulteriori informazioni in meritoBatchMeterUsage, consulta la BatchMeterUsagesezione AWS Marketplace Metering Service API Reference.

Esempio di risposta

{ "Results": [ { "UsageRecord": { "Timestamp": "2025-06-05T14:00:00Z", "CustomerIdentifier": "customerID", "CustomerAWSAccountId": "111122223333", "Dimension": "Dimension1", "Quantity": 10000, "LicenseArn": "arn:aws:license-manager::123456789012:license:l-exampleLicense12345", "UsageAllocations": [ { "AllocatedUsageQuantity": 5000, "Tags": [ { "Key": "user", "Value": "john" }, { "Key": "feature", "Value": "data-processing" }, { "Key": "job-name", "Value": "job11111" } ] }, { "AllocatedUsageQuantity": 2500, "Tags": [ { "Key": "user", "Value": "john" }, { "Key": "feature", "Value": "data-storage" }, { "Key": "cluster-name", "Value": "cluster11111" } ] }, { "AllocatedUsageQuantity": 2500, "Tags": [ { "Key": "user", "Value": "jane" }, { "Key": "feature", "Value": "data-storage" }, { "Key": "cluster-name", "Value": "cluster22222" } ] } ] }, "MeteringRecordId": "8fjef98ejf", "Status": "Success" } ], "UnprocessedRecords": [] }