Technical
Using the Digital Goods API for Google Play Billing in TWAs
October 4, 2026 · 8 min read
Integrating Google Play Billing with the Digital Goods API
For SaaS founders and indie makers deploying web applications to Android using a Trusted Web Activity, monetisation is a primary consideration. While standard web checkout platforms are excellent for normal browser-based traffic, publishing on Google Play requires compliance with platform billing rules for digital goods and services. To bridge the gap between standard web technologies and native Google Play Billing, Android and Chrome introduced the Digital Goods API.
The Digital Goods API works in tandem with the Payment Request API to allow web applications running inside a Trusted Web Activity to query product details, trigger purchase flows, and manage subscriptions directly through the Google Play Store billing engine. This article provides a comprehensive technical guide on how to integrate the Digital Goods API to enable native-like checkout experiences in your converted progressive web app.
Understanding the Digital Goods Architecture
When your progressive web app runs in a standard mobile browser, the Digital Goods API is unavailable because browsers do not have a secure connection to an app distribution platform billing system. However, when the app is packaged inside a Trusted Web Activity and installed from the Google Play Store, Android establishes a secure bridge. The browser environment detects the package context and links the digital goods requests to the Google Play Billing Library on the host device.
This flow relies on three core components working together. First, the Digital Goods API queries product details such as local currency prices and descriptions. Second, the Payment Request API handles the actual checkout overlay. Third, your backend application server verifies the purchase token with the Google Play Developer API to grant access to the purchased service or item. This setup ensures you comply with platform rules while maintaining a single web-based codebase.
Checking Support and Initialising the Service
Before executing any billing logic, your application must verify that the Digital Goods API is supported by the user agent and that the application is running within an active Trusted Web Activity. You can check for the presence of the digital goods service on the window object.
if (window.getDigitalGoodsService) { console.log('Digital Goods API is supported'); }
If the API is supported, you must request access to the specific digital goods service provider. For applications published on Google Play, the service provider identifier is a specific URL associated with the Play Store billing engine.
async function initializeBilling() { try { const service = await window.getDigitalGoodsService('https://play.google.com/billing'); return service; } catch (error) { console.error('Failed to connect to billing service', error); return null; } }
This asynchronous operation returns a service object which serves as the entry point for all subsequent billing tasks, including querying items and verifying purchases.
Querying Available Digital Products
Once you obtain an instance of the digital goods service, you can fetch product details for items configured in your Google Play Console. These products must be defined beforehand as either one-time products or recurring subscriptions within your application configuration in the play developer portal.
To retrieve details, pass an array of product IDs (also known as SKUs) to the getDetails method of the service object. This method returns details such as the localised price, currency, title, and description.
async function fetchProducts(service, productIds) { try { const details = await service.getDetails(productIds); details.forEach(product => { console.log('ID: ' + product.itemId); console.log('Title: ' + product.title); console.log('Price: ' + product.value + ' ' + product.currency); }); return details; } catch (error) { console.error('Error fetching products', error); return []; } }
Querying product details ensures that you always display accurate, local currency prices to your users without manually fetching exchange rates or maintaining a database of localised currency structures.
Processing Purchases and Subscriptions
Initiating a purchase requires transitioning from the Digital Goods API to the Payment Request API. The digital goods service acts as a validation tool, but the actual transaction overlay is managed by the browser payment broker.
You must construct a payment request object containing the payment method set to the Google Play billing identifier, alongside details about the specific product being purchased. The product identifier retrieved during the query phase must be passed in the payment method data object.
async function purchaseItem(productId) { const paymentMethods = [{ supportedMethods: 'https://play.google.com/billing', data: { sku: productId } }]; const paymentDetails = { total: { label: 'Total', amount: { currency: 'USD', value: '0.00' } } }; try { const request = new PaymentRequest(paymentMethods, paymentDetails); const response = await request.show(); const purchaseToken = response.details.purchaseToken; await verifyPurchaseOnBackend(purchaseToken); await response.complete('success'); } catch (error) { console.error('Purchase process failed', error); } }
The response object returned from the payment request containing the purchase token is critical. This token must be sent securely to your backend application server to prevent client-side bypass attacks.
Acknowledging and Consuming Purchases
Google Play requires all successful purchases to be explicitly acknowledged. If an app fails to acknowledge a purchase within three days, Google Play automatically voids the purchase and refunds the transaction to the user. This mechanism protects consumers from faulty integrations that fail to deliver purchased goods.
Acknowledgment can be handled on the web backend or directly in the frontend using the digital goods service. If the item is a consumable product, such as virtual credits, it must be consumed rather than simply acknowledged so that the user can buy it again in the future.
| Purchase Type | Required Action | Description |
|---|---|---|
| One-Time Entitlement | Acknowledge | Permanent access to a feature, premium upgrade, or ad-free mode. |
| Consumable Item | Consume | Used for items that can be purchased repeatedly, like tokens or credits. |
| Subscription | Acknowledge | Recurring access that must be verified on subscription renewal. |
Integrating these calls within your service layer ensures that purchases remain valid and that your financial logs match Google Play Console reporting.
Best Practices for Web Developers
To ensure a flawless implementation of the Digital Goods API inside your Trusted Web Activity, keep the following development principles in mind:
- Implement elegant fallback paths: If the Digital Goods API is not supported (for example, if a user accesses the app via a standard desktop browser), hide the native purchase buttons and display alternative checkout systems.
- Secure your validation logic: Never trust purchase status checks executed solely on the client side. Always verify purchase tokens against the Google Developer API using a secure server environment.
- Handle pending transactions: Google Play allows payment methods that take time to clear, such as bank transfers or cash points. Ensure your system can process pending states and grant access only when the payment succeeds.
By using the Digital Goods API, you preserve the single codebase advantages of progressive web apps while offering a fully integrated, platform-compliant purchase system for your Android user base.
Ready to ship your Android app?
Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.
Build my app