• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
tr3ntg

tr3ntg

readback-api

API for readback.
Unlisted
Like
readback-api
Home
Code
12
.claude
1
backend
5
docs
1
marketing
3
test
1
.vtignore
ENVIRONMENT_CONFIG.md
README.md
deno.json
main.tsx
test-upload.html
test-url-extraction.html
Branches
2
Pull requests
Remixes
History
Environment variables
7
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
backend
/
database
/
README.md
Code
/
backend
/
database
/
README.md
Search
9/22/2025
Viewing readonly version of develop branch: v1
View latest version
README.md

Database Module

This module handles usage tracking and credit management for the speech synthesis API, with support for per-request sandbox/production environment filtering.

Schema

customer_usage_v1 Table

ColumnTypeDescription
idINTEGER PRIMARY KEYAuto-incrementing unique identifier
customer_idTEXT NOT NULLCustomer ID from RevenueCat
character_countINTEGER NOT NULLNumber of characters in the request
request_timestampTEXT NOT NULLISO timestamp when request was made
created_atTEXT DEFAULT CURRENT_TIMESTAMPDatabase insertion timestamp

Indexes:

  • customer_id - For efficient customer lookups
  • request_timestamp - For efficient date range queries

credit_grants_v1 Table

ColumnTypeDescription
idINTEGER PRIMARY KEYAuto-incrementing unique identifier
customer_idTEXT NOT NULLCustomer ID from RevenueCat
sourceTEXT NOT NULLGrant source (free_grant, iap, admin, refund)
product_lookup_keyTEXTRevenueCat product ID for purchases
revenuecat_tx_idTEXTTransaction ID for idempotency
characters_grantedINTEGER NOT NULLCharacters granted (negative for refunds)
price_usdREALPrice paid for the credits
granted_atTEXT DEFAULT CURRENT_TIMESTAMPGrant timestamp
created_atTEXT DEFAULT CURRENT_TIMESTAMPDatabase insertion timestamp
is_consolidatedBOOLEAN DEFAULT FALSETracks alias consolidation
environmentTEXT DEFAULT 'PRODUCTION'Environment (SANDBOX or PRODUCTION)

Indexes:

  • customer_id - For efficient customer lookups
  • revenuecat_tx_id (UNIQUE) - For idempotent webhook processing
  • customer_id, is_consolidated - For alias consolidation queries

credit_debits_v1 Table

ColumnTypeDescription
idINTEGER PRIMARY KEYAuto-incrementing unique identifier
customer_idTEXT NOT NULLCustomer ID from RevenueCat
usage_idINTEGERForeign key to customer_usage_v1
characters_consumedINTEGER NOT NULLNon-expiring characters consumed
consumed_atTEXT DEFAULT CURRENT_TIMESTAMPConsumption timestamp

Indexes:

  • customer_id - For efficient customer lookups

Functions

initializeUsageTable()

Creates the usage tracking table if it doesn't exist. Called automatically when the module is imported.

recordUsage(customerId: string, characterCount: number)

Records a usage event for a customer.

getMonthlyUsage(customerId: string): Promise<number>

Returns the total character count used by a customer in the current calendar month.

getUsageHistory(customerId: string, limit?: number): Promise<UsageRecord[]>

Returns detailed usage history for a customer (useful for debugging or admin dashboards).

Usage Calculation

Monthly usage is calculated based on calendar months:

  • Start: 1st day of the month at 00:00:00
  • End: Last day of the month at 23:59:59.999

This means customers get a fresh allowance on the 1st of each month, regardless of when they subscribed.

Environment Filtering

The credit system supports per-request sandbox/production environment filtering:

  • Request Configuration: Environment is specified per-request via:
    • X-Environment header (e.g., X-Environment: SANDBOX)
    • Query parameter (e.g., ?environment=sandbox)
    • Default: PRODUCTION when not specified
  • Data Isolation: Sandbox and production credits are completely isolated
  • Webhook Processing: All webhook events are stored with their actual environment from RevenueCat
  • Query-Time Filtering: Environment filtering happens at query time based on the request's environment parameter

This allows for:

  • Complete audit trail of all transactions
  • Different clients using different environments simultaneously
  • Production clients work by default without code changes
  • Test devices can opt into sandbox mode on a per-request basis
  • No global configuration changes needed for testing

Performance Considerations

  • Indexes on customer_id and request_timestamp ensure fast queries
  • Monthly usage queries use date range filtering for efficiency
  • Table name includes version suffix (_v1) for easy schema migrations
  • Environment filtering uses indexed columns for efficient queries
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.