Back to Documentation
API Reference

Get Sitemap

Retrieve sitemap data for all published posts to generate XML sitemaps.

GET
client.getSitemap()
import { LightweightClient } from 'lightweight-client';

async function getSitemap() {
  const key = process.env.LIGHTWEIGHT_API_KEY;
  if (!key) throw Error('LIGHTWEIGHT_API_KEY environment variable must be set');

  const client = new LightweightClient(key);
  return client.getSitemap();
}

Parameters

This endpoint takes no parameters.

Response

[
  {
    "slug": "getting-started-with-nextjs",
    "lastModified": "2024-01-15T10:30:00.000Z",
    "category": "development"
  },
  {
    "slug": "designing-for-accessibility",
    "lastModified": "2024-01-14T08:00:00.000Z",
    "category": "design"
  },
  {
    "slug": "seo-best-practices",
    "lastModified": "2024-01-13T12:00:00.000Z",
    "category": "marketing"
  }
]

Response Schema

slugstring

URL-friendly identifier of the post

lastModifiedstring (ISO 8601)

Last modification date (uses publishedAt or updatedAt)

categorystring

Category slug of the post

Next.js Integration

Here's how to use the sitemap data to generate a dynamic sitemap in Next.js 13+:

// app/sitemap.ts
import { MetadataRoute } from 'next'
import { LightweightClient } from 'lightweight-client'

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const client = new LightweightClient(process.env.LIGHTWEIGHT_API_KEY!)
  const posts = await client.getSitemap()
  
  const blogPosts = posts.map((post) => ({
    url: `https://yourdomain.com/blog/${post.slug}`,
    lastModified: new Date(post.lastModified),
    changeFrequency: 'weekly' as const,
    priority: 0.8,
  }))

  return [
    {
      url: 'https://yourdomain.com',
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 1,
    },
    ...blogPosts,
  ]
}
Notes
  • Returns only published posts (posts with a publishedAt date)
  • Lightweight data format optimized for sitemap generation
  • Use with Next.js app/sitemap.ts for automatic XML generation