Load One Entry by ID
getEntry
`getEntry(collection, id)` fetches a single entry — useful for detail pages or following a reference.
What you'll learn
- Fetch one entry
- Handle the not-found case
getEntry(collection, id) fetches a single entry by id. Useful
inside a dynamic route’s getStaticPaths or to follow a reference
field.
The Pattern
---
// src/pages/authors/[id].astro
import { getEntry, getCollection } from "astro:content";
export async function getStaticPaths() {
const authors = await getCollection("authors");
return authors.map(a => ({ params: { id: a.id } }));
}
const { id } = Astro.params;
const author = await getEntry("authors", id);
---
<h1>{author.data.name}</h1>
<p>{author.data.bio}</p> Not Found
If the id doesn’t exist, getEntry returns undefined:
---
const author = await getEntry("authors", "missing");
if (!author) return Astro.redirect("/404");
--- In static mode, you’d usually have already filtered ids in
getStaticPaths, so this case is rare. In server mode, it’s
worth a defensive check.
Alternative — Pass a Reference
getEntry also accepts a reference object:
const author = await getEntry({ collection: "authors", id: "ada" }); Either form works. The first is shorter.
Up Next
Linking collections to each other — e.g., a post pointing at an author.
References →