Skip to main content
If your app runs a Mastra agent and users send it video or podcast links, the agent needs a way to read them. Fetching the media yourself tends to break once the app is deployed: sites block the datacenter IPs your server runs on, and many videos have no captions. Give the agent Importly tools instead. They call the REST API from your server and return the transcript, a download link for the file, or the video’s details. This page uses plain createTool() definitions, so the only packages you need are @mastra/core and zod.

1. Store your key

Create your key on the API key page (it is shown once) and add it to your server environment, next to your model provider’s key:
.env
The tools run where your Mastra server runs, so the key never reaches the browser.

2. Define the tools

Each tool starts an Importly job and polls its status until it finishes. A transcript of a video with captions usually comes back within the first request, because wait holds it open for up to 30 seconds.
src/mastra/tools/importly.ts
maxCostDollars caps what one call can spend. If a job would cost more, it fails with MAX_COST_EXCEEDED and the price instead of running. Raise the caps to suit your app. mode: 'auto' uses the video’s captions when it has them and speech-to-text when it doesn’t.

3. Give the tools to an agent

src/mastra/agents/media-agent.ts
Register it on your Mastra instance:
src/mastra/index.ts
Then call it from your app’s backend, for example to summarize the video behind a link:
src/summarize.ts
The agent calls transcribe-media, reads result.text, and answers in the next step. maxSteps lets it take more than one step, which it needs to use a tool result. Swap in any model your Mastra setup supports. The same agent works with agent.stream() for a chat UI, and in Mastra Studio while you develop. These examples typecheck against @mastra/core 1.85.

What each tool returns

The tools hand the job object back to the agent: A job that can’t finish, for example because the video is private, comes back with status: 'failed' and a code the agent can explain to the user (error codes). A long download may still be running when the tool stops polling. For files, a webhook is the better fit in production: start the import from your backend, store the jobId, and save mediaUrl when the webhook arrives. The Next.js guide shows that flow.

Costs

Each tool call is billed like the same API request: a caption transcript is $0.003, speech-to-text $0.01 a minute, and downloads are priced by size. See Billing. Importly is free to try, no card required.