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/articles

Response

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.org/blog/ai-revolutionizing-development</loc>
    <lastmod>2025-09-15T16:48:06.448Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://example.org/blog/quantum-computing-breakthrough</loc>
    <lastmod>2025-09-13T13:28:42.796Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://example.org/blog/future-of-web-development</loc>
    <lastmod>2025-09-10T09:15:32.234Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://example.org/blog/understanding-microservices</loc>
    <lastmod>2025-09-08T14:22:18.567Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <!-- Additional blog post URLs -->
</urlset>

XML Structure

Returns a standard XML sitemap conforming to the sitemaps.org protocol. Each URL entry contains:

locstring

Absolute URL of the blog post (baseUrl + post slug)

lastmodstring (ISO 8601)

Last modification date of the blog post

changefreqstring

Expected update frequency. Default is "weekly" for blog posts

prioritynumber

Crawl 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.xml
SEO 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-0bb1c2fe5772 for testing