Back to Documentation
API Reference
SEO
Get Sitemap
Generate an XML sitemap for all blog posts, perfect for search engine optimization (SEO).
GET
client.getSitemap(baseUrl)import { LightweightClient } from 'lightweight-client';
const BASE_BLOG_URL = "https://example.org/blog";
export async function GET() {
const key = process.env.LIGHTWEIGHT_API_KEY;
if (!key) {
throw Error('LIGHTWEIGHT_API_KEY environment variable must be set. You can use the DEMO key a8c58738-7b98-4597-b20a-0bb1c2fe5772 for testing - please set it in the root .env.local file.');
}
const client = new LightweightClient(key);
// The getSitemap method now returns the complete XML string
const sitemapXML = await client.getSitemap(BASE_BLOG_URL);
return new Response(sitemapXML, {
status: 200,
headers: {
'Cache-control': 'public, s-maxage=86400, stale-while-revalidate',
'content-type': 'application/xml',
},
});
}Parameters
baseUrl
string
required
The base URL of your blog. This will be prepended to all post slugs to create absolute URLs.
Example:
https://example.org/blog,https://myblog.com/articlesResponse
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>string</loc> <!-- Full URL to blog post -->
<lastmod>string</lastmod> <!-- ISO 8601 timestamp of last modification -->
<changefreq>string</changefreq> <!-- Update frequency (always, hourly, daily, weekly, monthly, yearly, never) -->
<priority>number</priority> <!-- Priority relative to other URLs (0.0 to 1.0) -->
</url>
<url>
<loc>string</loc>
<lastmod>string</lastmod>
<changefreq>string</changefreq>
<priority>number</priority>
</url>
<!-- ... more URL entries -->
</urlset>XML Structure
Returns a standard XML sitemap conforming to the sitemaps.org protocol. Each URL entry contains:
locstringAbsolute URL of the blog post (baseUrl + post slug)
lastmodstring (ISO 8601)Last modification date of the blog post
changefreqstringExpected update frequency. Default is "weekly" for blog posts
prioritynumberCrawl priority relative to other pages. Default is 0.8 for blog posts
Implementation Guide
Next.js App Router Setup
Create a route handler at app/sitemap.xml/route.ts:
// app/sitemap.xml/route.ts
import { LightweightClient } from 'lightweight-client';
const BASE_BLOG_URL = process.env.NEXT_PUBLIC_SITE_URL + '/blog';
export async function GET() {
const client = new LightweightClient(process.env.LIGHTWEIGHT_API_KEY!);
const sitemapXML = await client.getSitemap(BASE_BLOG_URL);
return new Response(sitemapXML, {
status: 200,
headers: {
'Cache-control': 'public, s-maxage=86400, stale-while-revalidate',
'content-type': 'application/xml',
},
});
}Robots.txt Configuration
Don't forget to add your sitemap to robots.txt:
User-agent: *
Allow: /
Sitemap: https://example.org/sitemap.xmlSEO Best Practices
- •The sitemap is automatically generated from all published posts
- •Submit your sitemap URL to Google Search Console and Bing Webmaster Tools
- •The sitemap updates automatically when you publish or update posts
- •Cache headers are set for 24 hours (
s-maxage=86400) to reduce API calls - •Maximum sitemap size is 50MB or 50,000 URLs per the sitemap protocol
- •Use the DEMO API key
a8c58738-7b98-4597-b20a-0bb1c2fe5772for testing