From Idea to Audio: Building the Podcast-It Cloudflare Worker

From Idea to Audio: Building the Podcast-It Cloudflare Worker

Introduction

Not so way back, I had the concept to show weblog posts into podcast episodes. My first few makes an attempt at this resulted in a handbook workflow the place the ultimate outcomes can be uploaded to YouTube. The stream was as follows:

  1. Copy the hyperlink to the article from the browser.
  2. Paste the hyperlink into NotebookLM.
  3. Wait for the audio to be generated.
  4. See if it was good. Otherwise, tweak the settings and repeat step 3.
  5. Once the audio was good, obtain it to my laptop computer.
  6. Next, use Headliner to create a video from the audio and add it to YouTube.

Needless to say, there was quite a lot of ready concerned on this course of. After doing this a pair occasions, I believed it will be attention-grabbing to attempt to automate it not directly.

In this put up, I’ll stroll by means of constructing podcast-it — a Cloudflare Worker that may take a weblog put up and return a playable audio file — and share the early selections, setup, and challenges alongside the way in which.

Choosing the Tech Stack

The major limitation was apparent instantly: NotebookLM doesn’t have an API, so there was no approach to automate in opposition to it. That meant I’d should recreate the important thing performance myself — from producing the narration script to producing the audio.

Since my web site already runs on Cloudflare, utilizing Cloudflare Workers felt like a pure match:

  • They run near customers, so responses are fast.
  • The free tier is beneficiant sufficient for small initiatives.
  • I may experiment with Workers past static web site internet hosting.

For the audio era, I selected OpenAI for each textual content era and text-to-speech synthesis. For storage, Cloudflare’s R2 handles the audio information, and D1 shops episode metadata.

Because the Worker would finally have to combine with my weblog, I constructed it as a REST API from the beginning — making it straightforward to name from my web site or another consumer.

Here’s the high-level objective I began with:

podcast-it flow sketch

Project Setup

Getting began with Workers was fairly easy. I began by studying up on their documentation, after which creating a replica of the backend API template utilizing Hono + Chanfana + D1 + Vitest template. This template was helpful as a result of it offers an OpenAPI web page, which is one thing I’m already accustomed to and discover fairly helpful for constructing REST APIs.

For reference, this is the command I used:

npm create cloudflare@newest -- --template=cloudflare/templates/chanfana-openapi-template

Additionally, I needed to set up the OpenAI SDK:

npm set up openai

With every little thing put in, I needed to replace the configurations within the wrangler.jsonc file. Specifically, I wanted so as to add bindings for R2 and D1:

"r2_buckets": [
{
"bucket_name": "podcasts",
"binding": "podcasts"
}
],
"d1_databases": [
{
"binding": "DB",
"database_name": "podcasts",
"database_id": "da7da40e-c9ac-4115-8773-1dcdeb8f4635",
"migrations_dir": "migrations"
}
],

For this venture, I additionally determined to take away the begin command from the package deal.json, and simply maintain the npm run dev command. I additionally modified the dev command to make use of a brand new migrate command to ensure the database can be up to date:

"migrate": "wrangler d1 migrations apply podcasts --local",
"dev": "npm run migrate && wrangler dev",

The migrate command applies the database scripts from the migrations/ listing. The listing holds SQL scripts which can be utilized to model how the database is constructed. See the D1 Migrations docs for extra data. I used to be not utilizing this strategy once I first began the venture, however I discovered it to be fairly helpful to have a approach to construct the database in the identical method throughout environments.

Additionally, I needed to make a checks/ listing since none existed once I first downloaded the template.

Once every little thing was arrange, I’d use the npm run dev to run the app and entry the OpenAPI web page at http://localhost:8787/.

Core Functionality: Text to Speech

For this venture, I went with OpenAI for each script writing and audio synthesizing. Rather than studying uncooked weblog textual content immediately, I generate a narration script first after which convert it to audio. This makes the outcome sound extra pure.

At a high-level, these are the elements concerned in creating the podcast episode:

podcast-it system components

Asynchronous Episode Creation

One key realization was that the episode creation endpoint wanted to be asynchronous. Audio era and storage can take a number of seconds, and blocking the request till every little thing finishes would create a poor expertise. Instead, when the Worker receives a POST /api/episodes request, it instantly returns a response with a slug whereas the Worker continues processing within the background.

A companion endpoint, GET /api/episodes/:slug, returns the present standing of that episode (e.g., pending, full) together with metadata as soon as the audio is full.

Here’s what the episode creation appears to be like like:

curl -X POST http://localhost:8787/api/episodes 
  -H "Content-Type: software/json" 
  -d '{
"slug": "making-a-podcast-app",
"content material": "Today I might be speaking about how I made a podcast era app. First, lets discuss...",
"hosts": [
{
"name": "string",
"voice": "alloy"
}
]
}'

The response physique simply repeats again the main points of the created episode and is returned with a 202 standing code:

{
"success": true,
"episode": {
"slug": "making-a-podcast-app",
"content material": "Today I might be speaking about how I made a podcast era app. First, lets discuss...",
"hosts": [
{
"name": "string",
"voice": "alloy"
}
],
"standing": "pending"
}
}

The 202 standing code indicators that the request was accepted however processing is just not but full.

Internal Flow

The steps contained in the Worker appear like this:

  1. Receive a weblog put up as uncooked textual content.
  2. Use OpenAI to generate a narration script.
  3. Send that script to OpenAI’s text-to-speech API.
  4. Store the ensuing audio in R2.
  5. Save metadata (standing, timestamps, transcript) in D1.
  6. Update the episode’s standing in D1 so the retrieval endpoint displays the newest state.

Episode Retrieval

Now, to retrieve the episode utilizing GET /api/episodes/:slug, the request appears to be like like this:

curl -X GET http://localhost:8787/api/episodes/making-a-podcast-app

When you retrieve an episode, the response displays its present state. At first, it might appear like this:

{
"success": true,
"episode": {
"slug": "making-a-podcast-app",
"audioFile": null,
"standing": "pending",
"transcript": null
}
}

Once processing finishes, it appears to be like like this:

{
"success": true,
"episode": {
"slug": "making-a-podcast-app",
"audioFile": "making-a-podcast-app.wav",
"standing": "full",
"transcript": "{"dialogue":[{"hostName":"string","dialogue":"Hi everybody, right this moment I’m diving into the method of constructing a podcast era app."},..."
}
}

In each circumstances, the response code is a 200.

Once the standing is full and the audioFile discipline is populated, the audio endpoint can be utilized to retrieve the precise audio:

curl -X 'GET' 
  'http://localhost:8787/api/audio/making-a-podcast-app.wav' 
  -H 'settle for: audio/mpeg'

Storing and Serving Episodes

Episodes are saved throughout two layers. The audio information themselves are positioned in R2 object storage, which offers low cost, scalable storage and permits the Worker to serve audio immediately with out worrying about persistence. Each audio file is saved utilizing the episode slug as the important thing, so for instance making-a-podcast-app.wav maps on to /api/episodes/making-a-podcast-app. The metadata for every episode — together with slug, transcript, timestamps, and present standing (pending or full) — lives in D1, making it straightforward for the retrieval endpoint to reply with structured JSON with out embedding the precise audio.

When a consumer requests an audio file by means of GET /api/audio/:filename, the Worker streams the file immediately from R2 again to the consumer. D1 is just ever queried for metadata, and R2 is just ever queried for uncooked binary audio. This separation of obligations turned out to be essential. Without it, I’d have needed to encode audio as base64 in JSON responses, which might be gradual and bloated. By splitting storage on this method, the Worker can serve quick metadata lookups whereas nonetheless delivering giant audio information effectively.

Here’s an instance of downloading the audio file with curl:

curl -X GET http://localhost:8787/api/audio/making-a-podcast-app.wav 
  -H 'settle for: audio/mpeg' --output making-a-podcast-app.wav

Testing the Worker

The integration checks train the actual endpoints with OpenAI mocked. Just a few issues they confirm:

  • A non-existent route returns 404 with the plain textual content physique "404 Not Found".
  • GET /api/episodes:

    • Returns 200 with an empty record when the desk is cleared.
    • After inserting one row immediately into D1, returns 200 with a single episode whose slug, standing, audioFile, and transcript match the row.
  • POST /api/episodes (episode creation):

    • Mocks openai.responses.parse to return a minimal dialogue and openai.audio.speech.create to return a 64-byte audio buffer.
    • Returns 202 on creation.
    • A subsequent GET /api/episodes/:slug returns 200 with standing: "full" and audioFile: "<slug>.wav".
    • GET /api/audio/<audioFile> returns 200 with binary audio; the byte size matches the mocked buffer and the primary few bytes are similar.
  • Duplicate creation is rejected:
    • A second POST /api/episodes with the identical slug returns 409 with the physique "Conflict: episode already exists".

The checks reset state the place wanted (e.g., deleting from Episodes earlier than record assertions) and deal with observable conduct: right standing codes, payload fields, and that audio retrieval returns actual bytes. There’s additionally a TODO so as to add an "error" standing path within the database for failure circumstances.

Unfortunately, I did encounter some difficulties when attempting to make use of Vitest with the Workers framework. Specifically, I used to be unable to create a __mocks__ listing, and I additionally discovered Vitest’s watch mode doesn’t work with D1.

For the __mocks__ workaround, my check file has the next mock created on the prime:

vi.mock("openai", () => {
  class OpenAI {
    constructor(_: any) { }
    get responses() {
      return { parse: responsesParse };
    }
    get audio() {
      return { speech: { create: audioCreate } };
    }
  }
  return { default: OpenAI };
});

As far as watch mode goes, there is not a workaround for me in the meanwhile. I suppose I may attempt to create a mock D1 occasion, however that will require extra time than I wish to put money into it. For these fascinated by these explicit issues, I discovered the next points had been reported on GitHub:

Conclusion

Building podcast-it gave me an opportunity to discover Cloudflare Workers past static internet hosting and actually stress-test R2 + D1 collectively. The greatest takeaway was how a lot easier the structure grew to become as soon as I break up audio storage and metadata storage — attempting to jam every little thing into one place would have been painful.

The venture continues to be tough across the edges, nevertheless it’s already helpful for rapidly turning writing into audio. In a follow-up, I’ll present the right way to wire podcast-it immediately into my web site’s publishing workflow.

If you’d prefer to dig into the code or strive it your self, the repo is open supply: github.com/h93xV2/podcast-it. Feedback, bug studies, and concepts are very welcome.

HI-FI News

by way of DEV Community https://dev.to

August 17, 2025 at 02:17AM