15-Minute Signed Audio Links: How Mantrilo Delivers Mantra Audio Securely
5 min read
Stream Sanskrit mantra audio in your app with a mantra audio API. How signed links work, redirect vs JSON mode, and a player that never breaks mid-chant.
Audio is what makes a devotional app feel alive, and it is the part most scripture APIs leave out. Mantrilo includes it. A user opens the app before puja, taps the Gayatri Mantra, and expects it to play cleanly from the first syllable to the last. Behind that tap is a question every developer eventually faces: how do you serve chant audio without exposing your files, your bandwidth bill or your API key?
Mantrilo's answer is the signed audio link. This post explains how the mantra audio API works, why every link expires after 15 minutes, and how to build a player around it that never fails halfway through a chant.
The one-request version
curl "https://api.mantrilo.com/v1/mantras/{id}/audio?format=json" \
-H "Authorization: Bearer $MANTRILO_API_KEY"You get back JSON containing a signed URL for that mantra's recording. The URL works for 15 minutes. After that it stops working, and you ask for a new one.
Want to hear it? Get a free sandbox key. Sandbox keys can play sample audio.
Why not just use public audio URLs?
The simplest way to ship audio is a public link like https://cdn.example.com/gayatri.mp3. It works on day one and causes three problems by month three.
Hotlinking. Anyone who finds the URL can embed it in their own app or site. You pay for their traffic.
No usage control. You can't tell which customer or which key played what, so you can't meter it, rate-limit it or revoke it.
Licensing. Recordings have rights attached. A permanent public URL turns a licensed recording into a free download.
A signed URL fixes all three. It is a normal HTTPS link with a cryptographic signature and an expiry time built into it. Storage only serves the file if the signature is valid and the time has not passed. Nobody can forge one without the signing secret, and a leaked link dies on its own within minutes.
How the Mantrilo audio endpoint works
GET /v1/mantras/{id}/audio has two modes.
| Mode | Request | What you get | Best for |
|---|---|---|---|
| Redirect | /v1/mantras/{id}/audio |
An HTTP redirect to the signed URL | Quick tests, server-side downloads |
| JSON | /v1/mantras/{id}/audio?format=json |
JSON with the signed URL | Apps and players |
In redirect mode, an HTTP client that follows redirects ends up with the audio bytes. That's convenient for a quick curl -L, but in an app you usually want the link itself, so you can hand it to an audio player. Use JSON mode for that.
Both modes need your API key in the Authorization header. That has an important consequence: a browser <audio> tag cannot call Mantrilo directly, because you can't attach a secret header to it and you must never ship the key to the client anyway. Your server sits in between.
The pattern: your server issues the link, the player streams it
The cleanest design is a tiny endpoint on your server that fetches a fresh signed link and redirects the player to it.
// server/audio.js
import express from "express";
const router = express.Router();
router.get("/api/mantras/:id/audio", async (req, res) => {
const id = encodeURIComponent(req.params.id);
const r = await fetch(`https://api.mantrilo.com/v1/mantras/${id}/audio?format=json`, {
headers: { Authorization: `Bearer ${process.env.MANTRILO_API_KEY}` },
});
const json = await r.json();
if (!r.ok) return res.status(r.status).json(json);
res.set("Cache-Control", "no-store"); // never let a browser or CDN keep the link
res.redirect(302, json.data.url);
});
export default router;Now your player can point at your own URL:
<audio id="player" preload="none" src="/api/mantras/gayatri/audio" controls></audio>When the user presses play, the browser calls your endpoint, gets redirected to a fresh signed URL and streams the file straight from storage. Your server never touches the audio bytes, so it stays cheap and fast.
Handling expiry mid-playback
Fifteen minutes is longer than most mantras. It is not longer than a 108-repetition japa track, a long stotram or a user who pauses and comes back after lunch.
Browsers stream audio in ranges. If a link expires and the player asks for the next range, storage refuses and the <audio> element fires an error event. The fix is to fetch a fresh link and resume from the same position:
const player = document.getElementById("player");
const mantraId = "gayatri";
player.addEventListener("error", () => {
const resumeAt = player.currentTime;
const wasPlaying = !player.paused || resumeAt > 0;
player.src = `/api/mantras/${mantraId}/audio?t=${Date.now()}`; // new link
player.addEventListener(
"loadedmetadata",
() => {
player.currentTime = resumeAt;
if (wasPlaying) player.play();
},
{ once: true }
);
});The t parameter makes the browser treat it as a new request instead of reusing the old redirect. Users hear at most a brief pause.
In native apps the idea is identical. On iOS, observe AVPlayerItem failures. On Android with ExoPlayer, catch the playback error. In both cases: request a new link from your server, swap the item, seek to the saved position.
Never cache the signed URL
It is tempting to store the link to save a request. Don't. A cached link is a broken link waiting to happen: the user who presses play at minute sixteen gets silence.
What you can keep is everything around the audio. Mantra text, transliteration, deity and title come from GET /v1/mantras/{id} and change rarely. Store those, and request the audio link only at the moment of playback.
Browsing mantras by deity
To build a library screen, list mantras with the catalog endpoint. It supports limit, cursor and a deity filter:
curl "https://api.mantrilo.com/v1/mantras?deity=shiva&limit=20" \
-H "Authorization: Bearer $MANTRILO_API_KEY"Use the cursor from each response to load the next page as the user scrolls. Render the list from this data, and only call the audio endpoint when someone taps play.
How audio counts against your plan
Each Mantrilo plan includes a monthly audio allowance in gigabytes, alongside its request allowance. The signed link is counted by the file size when the link is issued, whether or not the user listens to the end.
Three habits keep that number honest:
- Use
preload="none"so pages with many players don't issue links nobody uses. - Issue links on play, not on screen load or hover.
- Reuse a live link within the same session. If the user pauses and resumes within a few minutes, the element keeps its current source; only re-request after an error.
A complete checklist
- API key on the server only
- JSON mode on the server, redirect to the signed URL for the player
Cache-Control: no-storeon your redirect endpointpreload="none"on players- Resume-on-error logic that fetches a new link and seeks
- Metadata cached, signed URLs never cached
Why use Mantrilo for mantra audio
Recording, licensing, hosting and securing chant audio is a project of its own. Mantrilo gives you a mantra catalog with recordings, delivered through signed links, metered against a clear audio allowance on every plan.
The same key covers the text around the audio: Devanagari and transliteration for each mantra, plus the Bhagavad Gita, Upanishads and astrology. Your library screen, your player and your daily verse all come from one provider.
FAQ
Why do Mantrilo audio links expire after 15 minutes? Short expiry stops leaked links from being reused, protects licensed recordings and keeps usage tied to your key.
Can I download the audio for offline use? Offline and download rights depend on your plan and agreement. Check your terms before storing audio files on devices.
Can I play mantra audio in a web page?
Yes. Point an <audio> element at your own server endpoint, which redirects to a fresh signed link. Never call Mantrilo from the browser with your key.
Does the sandbox include audio? Sandbox keys can play sample audio, so you can build and test your full player before choosing a plan.
Start with sample audio today. Get a free sandbox key and read the mantra endpoints in the docs.
