Listen Notes Podcast API JavaScript Library
The `podcast-api` library provides convenient JavaScript bindings for the Listen Notes Podcast API, enabling developers to search, retrieve, and manage podcast and episode metadata programmatically. It supports a wide range of environments, including server-side Node.js applications, serverless Cloudflare Workers/Pages, and client-side browser JavaScript, although browser usage is discouraged without proper API key handling to prevent exposure. The current stable version is 2.0.4, with active development indicated by frequent releases introducing new endpoints like `searchEpisodeTitles` and `fetchPodcastsByDomain`. A key differentiator is its dual client architecture, offering `Client` for Node.js/browser environments and `ClientForWorkers` specifically optimized for Cloudflare Workers, providing streamlined access to the same API that powers the Listen Notes search engine.
Common errors
-
API key is missing
cause The `apiKey` parameter was not provided during client initialization, or the `process.env.LISTEN_API_KEY` (or `LISTEN_API_KEY` for Workers) environment variable was not set.fixInitialize the client with `Client({ apiKey: 'YOUR_API_KEY' })` or ensure your `LISTEN_API_KEY` environment variable is correctly configured and accessible in your application's environment. -
TypeError: (0 , podcast_api__WEBPACK_IMPORTED_MODULE_0__.Client) is not a function
cause This error often occurs in environments using module bundlers or transpilers when a default import is attempted for a library that only provides named exports, or when CommonJS `require` is used and then the result is incorrectly called directly as a function.fixEnsure you are using named imports for ESM (`import { Client } from 'podcast-api';`) or destructuring for CommonJS (`const { Client } = require('podcast-api');`). Do not attempt `import Client from 'podcast-api';` or `const Client = require('podcast-api'); Client(...)`.
Warnings
- gotcha Directly using the Podcast API client with an exposed API key in client-side browser JavaScript is strongly discouraged. This can lead to your API key being compromised.
- gotcha Two distinct client classes exist: `Client` for Node.js/browser environments and `ClientForWorkers` for Cloudflare Workers/Pages functions. Using the wrong client can lead to unexpected behavior or errors, especially concerning API key handling.
- gotcha The library requires Node.js version 10 or higher. Running it on older Node.js versions may result in compatibility issues or runtime errors.
Install
-
npm install podcast-api -
yarn add podcast-api -
pnpm add podcast-api
Imports
- Client
const Client = require('podcast-api');import { Client } from 'podcast-api'; const client = Client({ apiKey: process.env.LISTEN_API_KEY ?? '' }); - ClientForWorkers
const { ClientForWorkers } = require('podcast-api');import { ClientForWorkers } from 'podcast-api'; const client = ClientForWorkers({ apiKey: LISTEN_API_KEY }); - Client
const { Client } = require('podcast-api'); const client = Client({ apiKey: process.env.LISTEN_API_KEY ?? '' });
Quickstart
import { Client } from 'podcast-api';
// Initialize the client with your API key.
// For Node.js, use process.env.LISTEN_API_KEY.
// For Cloudflare Workers, use LISTEN_API_KEY from secrets.
// For browser-side, handle key securely (e.g., via a proxy).
const apiKey = process.env.LISTEN_API_KEY ?? 'YOUR_LISTEN_NOTES_API_KEY'; // Replace or ensure env var is set
if (!apiKey || apiKey === 'YOUR_LISTEN_NOTES_API_KEY') {
console.error('API Key is missing. Please set LISTEN_API_KEY environment variable or replace the placeholder.');
process.exit(1);
}
const client = Client({ apiKey });
async function searchPodcasts(query) {
try {
const response = await client.search({
q: query,
sort_by: 'relevance',
type: 'podcast',
offset: 0,
len_min: 10,
len_max: 30,
genre_ids: '68,82',
published_before: 1580172454000,
published_after: 0,
only_in: 'title,description',
language: 'English',
safe_mode: 0
});
console.log(`Found ${response.data.total} podcasts for "${query}":`);
response.data.results.slice(0, 3).forEach(podcast => {
console.log(`- ${podcast.title_original} (ID: ${podcast.id})`);
});
} catch (error) {
console.error('Error searching podcasts:', error.message);
}
}
// Example usage
searchPodcasts('web development');