saveTextToPodcast
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.
Viewing readonly version of main branch: v56View latest version
This HTTP Val converts text files to speech using OpenAI's TTS API and stores the results for podcast generation.
- Set the
OPENAI_API_KEYenvironment variable in your Val Town settings - Set the
AUTH_TOKENenvironment variable for API authorization - The SQLite database table will be created automatically on first use
- Visit
/testfor a web interface to upload and convert text files - Or use the API endpoints directly (see below)
Web interface for testing file uploads and conversions.
Converts a text file to speech and stores it. Requires authorization.
Headers:
Authorization: Bearer YOUR_AUTH_TOKEN
Parameters:
file(File): Text file to convertname(string): Name/title for the episode
Example using curl:
curl -X POST https://your-val-url.web.val.run/convert \ -H "Authorization: Bearer your-auth-token-here" \ -F "file=@your-text-file.txt" \ -F "name=My First Episode"
Response:
{ "success": true, "message": "Text converted to speech successfully", "data": { "name": "My First Episode", "mp3_url": "https://your-val-url.web.val.run/audio/tts_1234567890.mp3", "text_length": 1250 } }
Serves MP3 audio files from storage.
Lists all converted episodes.
Response:
{ "success": true, "episodes": [ { "id": 1, "name": "My First Episode", "text": "This is the text content...", "mp3_url": "https://your-val-url.web.val.run/audio/tts_1234567890.mp3", "date_added": "2024-01-01 12:00:00" } ] }
Get database statistics and cleanup information.
Response:
{ "success": true, "stats": { "totalEpisodes": 5, "recentEpisodes": 3, "oldEpisodes": 2, "oldestEpisode": { "name": "First Episode", "date_added": "2024-01-01T12:00:00.000Z" }, "newestEpisode": { "name": "Latest Episode", "date_added": "2024-01-15T12:00:00.000Z" }, "cleanupInfo": { "eligibleForCleanup": 2, "cleanupThreshold": "30 days", "nextCleanup": "Weekly (automated via cron)" } } }
Manually trigger cleanup of old episodes. Requires authorization.
Headers:
Authorization: Bearer YOUR_AUTH_TOKEN
Body (optional):
{ "days": 30 }
Parameters:
days(number, optional): Delete episodes older than this many days (1-365, default: 30)
Response:
{ "success": true, "message": "Cleanup completed for episodes older than 30 days", "result": { "deletedRecords": 2, "deletedFiles": 2, "failedFiles": 0, "deletedEpisodes": [ {"id": 1, "name": "Old Episode 1"}, {"id": 2, "name": "Old Episode 2"} ] } }
Generates an RSS podcast feed from all episodes. Requires authorization.
Query Parameters:
key(string): Must match the AUTH_TOKEN environment variable
Example:
https://your-val-url.web.val.run/podcast.xml?key=your-auth-token-here
The tts_episodes table contains:
id(INTEGER PRIMARY KEY)name(TEXT) - Episode titletext(TEXT) - Original text contentmp3_url(TEXT) - URL to the MP3 filedate_added(DATETIME) - Timestamp when added
- ✅ Text file upload and conversion to MP3
- ✅ SQLite database storage
- ✅ MP3 file serving from blob storage
- ✅ RSS podcast feed generation
- ✅ Authorization protection for content creation and feed access
- ✅ Error handling and validation
- ✅ Automatic database initialization
- ✅ Web test interface
A weekly cron job (/cleanup.ts) automatically deletes:
- Database records older than 30 days
- Associated MP3 files from blob storage
- Runs every week to keep storage usage manageable
- ✅ Deletes episodes older than 30 days
- ✅ Removes both database records and MP3 files
- ✅ Detailed logging of cleanup operations
- ✅ Error handling and reporting
- ✅ Runs weekly (schedule configurable in Val Town UI)
- Use
POST /cleanupendpoint for immediate cleanup - Configurable age threshold (1-365 days)
- Same authorization as content creation
- Detailed cleanup reports
- POST /convert: Requires
Authorization: Bearer YOUR_TOKENheader - GET /podcast.xml: Requires
?key=YOUR_TOKENquery parameter - Both use the same
AUTH_TOKENenvironment variable for validation - All other endpoints (episodes list, audio serving, test page) are public
- Uses OpenAI's
tts-1model with thealloyvoice - MP3 files are stored in Val Town's blob storage
- Database records include full text content for search/reference
- Podcast feed is automatically generated from database records
- All endpoints include proper error handling