Why Inventory Management Is Critical for Digital Goods
Unlike physical goods, digital gift cards look like they can be sold in unlimited quantities โ but supplier stock is finite. An oversell incident (taking a customer's money for a code that doesn't exist) destroys trust. Proper inventory management ensures you only sell what you can deliver instantly.
With FoxReload's API, stock levels are part of every catalog response, and you can build a live inventory layer that prevents overselling before it happens.
Understanding Stock Data from the API
Every product in the FoxReload catalog includes a stock field representing the number of codes currently available.
{
"id": "gp-usd-25",
"name": "Google Play Gift Card $25",
"stock": 143,
"delivery_type": "instant"
}
delivery_type: "instant" means codes are fulfilled the moment an order is placed โ no waiting for manual processing.
Step 1 โ Maintain a Local Stock Mirror
Polling the API every 10โ15 minutes gives you a near-real-time view of supplier stock. Store this in a local table to avoid latency on every storefront page load.
CREATE TABLE inventory_snapshot (
product_id VARCHAR(64) PRIMARY KEY,
stock INT NOT NULL,
last_synced TIMESTAMP NOT NULL
);
def refresh_inventory(conn):
r = requests.get(
"https://api.foxreload.com/api/v1/catalog",
headers={"Authorization": f"Bearer {API_KEY}"},
params={"category": "google-play"}
)
products = r.json()["products"]
cur = conn.cursor()
for p in products:
cur.execute("""
INSERT INTO inventory_snapshot (product_id, stock, last_synced)
VALUES (%s, %s, NOW())
ON CONFLICT (product_id) DO UPDATE
SET stock=EXCLUDED.stock, last_synced=EXCLUDED.last_synced
""", (p["id"], p["stock"]))
conn.commit()
Step 2 โ Implement a Code Reservation System
Between the moment a customer clicks "Buy" and the moment the order is confirmed, you must reserve the stock to prevent two concurrent buyers from getting the same item.
Use optimistic locking or a short-lived reservation:
CREATE TABLE order_reservations (
reservation_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
product_id VARCHAR(64) NOT NULL,
quantity INT NOT NULL DEFAULT 1,
reserved_at TIMESTAMP NOT NULL DEFAULT NOW(),
expires_at TIMESTAMP NOT NULL, -- reserved_at + 5 minutes
order_id VARCHAR(64) -- set when order completes
);
import uuid
from datetime import datetime, timedelta
def reserve_stock(conn, product_id: str, quantity: int = 1) -> str | None:
cur = conn.cursor()
# Check effective available stock
cur.execute("""
SELECT
s.stock - COALESCE(SUM(r.quantity), 0) AS available
FROM inventory_snapshot s
LEFT JOIN order_reservations r
ON r.product_id = s.product_id
AND r.expires_at > NOW()
AND r.order_id IS NULL
WHERE s.product_id = %s
GROUP BY s.stock
""", (product_id,))
row = cur.fetchone()
if not row or row[0] < quantity:
return None # Out of stock
reservation_id = str(uuid.uuid4())
cur.execute("""
INSERT INTO order_reservations (reservation_id, product_id, quantity, expires_at)
VALUES (%s, %s, %s, %s)
""", (reservation_id, product_id, quantity,
datetime.utcnow() + timedelta(minutes=5)))
conn.commit()
return reservation_id
Step 3 โ Release Reservations on Timeout
Expired reservations (e.g., customer abandoned checkout) must be cleaned up so stock returns to the available pool.
-- Run every minute via pg_cron or a cron job
DELETE FROM order_reservations
WHERE expires_at < NOW() AND order_id IS NULL;
Step 4 โ Set Low-Stock Alerts
Proactively warn yourself when a popular SKU is running low so you can restock before a stockout affects customers.
LOW_STOCK_THRESHOLD = 20 # codes
def check_low_stock(conn):
cur = conn.cursor()
cur.execute("""
SELECT product_id, stock FROM inventory_snapshot
WHERE stock < %s AND stock > 0
""", (LOW_STOCK_THRESHOLD,))
low = cur.fetchall()
for product_id, stock in low:
send_alert(f"Low stock: {product_id} has only {stock} codes left.")
Step 5 โ Handle Out-of-Stock Gracefully
When stock reaches 0, hide the product from your storefront or show an "out of stock" badge โ never let customers complete checkout for a product you can't fulfill.
def is_available(conn, product_id: str) -> bool:
cur = conn.cursor()
cur.execute(
"SELECT stock FROM inventory_snapshot WHERE product_id=%s",
(product_id,)
)
row = cur.fetchone()
return row is not None and row[0] > 0
Step 6 โ Reconcile After Order Fulfillment
After an order is fulfilled (codes delivered), decrement your local stock mirror to avoid showing stale high counts between sync cycles.
def on_order_fulfilled(conn, product_id: str, quantity: int, reservation_id: str):
cur = conn.cursor()
# Mark reservation as completed
cur.execute(
"UPDATE order_reservations SET order_id=%s WHERE reservation_id=%s",
(order_id, reservation_id)
)
# Decrement local stock mirror
cur.execute(
"UPDATE inventory_snapshot SET stock = stock - %s WHERE product_id=%s",
(quantity, product_id)
)
conn.commit()
Inventory Health Dashboard
Consider building a simple admin view that shows:
| Metric | Description |
|---|---|
| Total active SKUs | Products with stock > 0 |
| Low-stock SKUs | Products below threshold |
| Pending reservations | Open reservations not yet fulfilled |
| Daily sold volume | Orders fulfilled in last 24 h |
| Stockout incidents | Orders rejected due to no stock |
Summary
A solid inventory system for Google Play Gift Cards requires four things: a synced local mirror of supplier stock, a reservation layer that prevents concurrent oversells, automated low-stock alerts, and clean reconciliation after each fulfilled order.
See Also
- How to connect Google Play Gift Cards to your shop via API
- How to implement instant delivery for Google Play Gift Cards
- How to set up webhooks for digital code delivery
Never oversell again. The FoxReload API gives you live stock data โ build your inventory layer on top and deliver every order instantly.

