To use CSV data in Astro, load the file with the file() loader and give it a parser that turns CSV text into an array of objects, such as csv-parse/sync with columns: true. Each row becomes a collection entry, validated by your schema, which you can list on a page or turn into one page per row with getStaticPaths().
Key Takeaways
file()doesn’t parse CSV itself; itsparseroption accepts any function returning an array.- Every row needs a unique
idcolumn (orslug).- CSV values are strings, so use
z.coerce.number(),z.coerce.date()and similar in the schema.- A spreadsheet exported as CSV lets non-developers edit site data.
Set up the collection
npm install csv-parse
id,name,city,category,price,opened
lantern-cafe,Lantern Cafe,Leeds,Cafe,12.50,2019-04-01
harbor-bistro,Harbor Bistro,Bristol,Restaurant,28,2021-09-15
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { file } from 'astro/loaders';
import { z } from 'astro/zod';
import { parse } from 'csv-parse/sync';
const venues = defineCollection({
loader: file('src/data/venues.csv', {
parser: (text) => parse(text, { columns: true, skip_empty_lines: true, trim: true }),
}),
schema: z.object({
name: z.string(),
city: z.string(),
category: z.enum(['Cafe', 'Restaurant', 'Bar']),
price: z.coerce.number(),
opened: z.coerce.date(),
}),
});
export const collections = { venues };
columns: true uses the header row as keys, so each row becomes { id, name, city, ... }. The id column becomes the entry ID. Everything in a CSV is text, so z.coerce converts prices and dates, and z.enum catches a misspelled category before it reaches the site. A bad row fails the build with the row’s ID in the error. The Zod frontmatter guide covers more schema rules.
List the rows
---
// src/pages/venues/index.astro
import { getCollection } from 'astro:content';
const venues = (await getCollection('venues')).sort((a, b) => a.data.name.localeCompare(b.data.name));
---
<table>
<thead><tr><th>Name</th><th>City</th><th>Average price</th></tr></thead>
<tbody>
{venues.map(({ id, data }) => (
<tr>
<td><a href={`/venues/${id}/`}>{data.name}</a></td>
<td>{data.city}</td>
<td>{data.price.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' })}</td>
</tr>
))}
</tbody>
</table>
One page per row
---
// src/pages/venues/[id].astro
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const venues = await getCollection('venues');
return venues.map((venue) => ({ params: { id: venue.id }, props: { venue } }));
}
const { data } = Astro.props.venue;
---
<h1>{data.name}</h1>
<p>{data.category} in {data.city}, open since {data.opened.getFullYear()}.</p>
This is a small directory site built from a spreadsheet. Our getStaticPaths guide explains params and props.
Spreadsheet tips
- Google Sheets or Excel: export as CSV into
src/data/and commit it. Editors update the sheet, you re-export, and the build validates the result. - No id column? Generate one in the parser:
parse(text, { columns: true }).map((row) => ({ ...row, id: slugify(row.name) })), with your ownslugifyfunction. Keep IDs stable, since they’re URLs. - Empty cells come through as empty strings. Use
z.string().optional()with atransformthat turns''intoundefined, orz.coerce.number().optional()only after removing blanks in the parser. - Semicolon-separated files (common in European exports): pass
delimiter: ';'toparse. - Live data: to read a published sheet’s CSV URL instead of a file, write an inline loader that fetches it at build time; the fetch at build time tip shows how.
For JSON and YAML, file() needs no parser at all; see the JSON and YAML data tip and the Content Collections docs page. Directory-style restaurant themes are a natural fit for CSV-driven listings.
FAQ
Can Astro read CSV files?
Yes, with a parser. Pass a function such as csv-parse/sync’s parse to the file() loader’s parser option, and each row becomes a content collection entry validated by your schema.
Why are my CSV numbers strings in Astro?
CSV has no data types, so every value is parsed as text. Use z.coerce.number() and z.coerce.date() in the collection schema, or the cast option in csv-parse, to convert them.
Can I use Google Sheets as a data source for Astro?
Yes. Export the sheet as CSV into your project and load it with file(), or fetch the sheet’s published CSV URL in a custom loader during the build. Rebuild the site when the sheet changes.