Examples & Code Snippets

Practical implementations for generating, submitting, and managing sitemaps across different platforms and languages.

Basic XML Sitemap

The foundational structure compliant with W3C Sitemap Protocol 0.9. Use this static format for small sites or manual deployments.

sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2025-01-15</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://example.com/blog</loc>
    <lastmod>2025-01-14</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
</urlset>
sitemap.html
<html>
  <head>
    <title>HTML Sitemap</title>
    <meta name="robots" content="noindex">
  </head>
  <body>
    <ul>
      <li><a href="/>Home</a></li>
      <li><a href="/blog>Blog</a></li>
    </ul>
  </body>
</html>
Note: XML sitemaps are meant for crawlers. HTML sitemaps improve user navigation and should be placed at /sitemap.html or /sitemap.

Dynamic Generation with Node.js

Automate sitemap creation for dynamic routes using our official SDK. Ideal for Next.js, Express, or custom server setups.

generate-sitemap.js
import { SitemapClient } from '@sitemap/sdk';

const client = new SitemapClient({
  apiKey: process.env.SITEMAP_API_KEY,
  siteUrl: 'https://example.com'
});

// Fetch dynamic routes from your DB/API
const urls = await getDynamicRoutes();

// Generate & submit atomically
const response = await client.generateAndSubmit({
  urls,
  format: 'xml',
  compress: true // Gzip enabled
});

console.log(`Submitted ${response.totalUrls} URLs`);
generate.py
import sitemap_sdk

client = sitemap_sdk.Client(
    api_key="your_api_key_here",
    site_url="https://example.com"
)

urls = [
    sitemap_sdk.Url(loc="/products/123", priority=0.9),
    sitemap_sdk.Url(loc="/products/456", lastmod="2025-01-15")
]

client.submit(urls, format="xml")
print("Sitemap updated successfully.")
submit.sh
curl -X POST "https://api.sitemap.xml/v2/submit" \n  -H "Authorization: Bearer $SITEMAP_KEY" \n  -H "Content-Type: application/json" \n  -d '{
    "site": "https://example.com",
    "urls": ["/home", "/about", "/contact"]
  }'

CMS Integrations

Quick-drop configurations for popular content management systems. These snippets automatically hook into your publication lifecycle.

functions.php
add_action('save_post', 'update_sitemap_on_publish', 10, 2);
function update_sitemap_on_publish($post_id, $post) {
    if ($post->post_status === 'publish') {
        wp_remote_post('https://api.sitemap.xml/v2/submit', [
            'body' => ['urls' => [get_permalink($post_id)]]
        ]);
    }
}
snippets/sitemap.liquid
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for product in collections.all.products %}>
    <url>
      <loc>{{ shop.url | append: product.url }}</loc>
      <lastmod>{{ product.updated_at | date: '%Y-%m-%d' }}</lastmod>
    </url>
  {% endfor %}>
</urlset>

Video & Image Extensions

Enhance your sitemap with rich media metadata to improve SERP snippets and media discovery.

video-sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
       xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
  <url>
    <loc>https://example.com/video-tutorial</loc>
    <video:video>
      <video:thumbnail_loc>https://example.com/thumb.jpg</video:thumbnail_loc>
      <video:title>How to Build a Sitemap</video:title>
      <video:description>A step-by-step guide</video:description>
      <video:content_loc>https://example.com/video.mp4</video:content_loc>
      <video:duration>300</video:duration>
    </video:video>
  </url>
</urlset>
image-sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
       xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>https://example.com/product/123</loc>
    <image:image>
      <image:loc>https://example.com/img/123-front.jpg</image:loc>
      <image:title>Product 123 Front</image:title>
      <image:caption>High-resolution product shot</image:caption>
    </image:image>
  </url>
</urlset>

Indexing API Submission

Bypass standard crawling queues and push critical URLs directly to search engine indexation pipelines.

api/submit.js
import { IndexClient } from '@sitemap/indexer';

const indexer = new IndexClient(process.env.INDEX_API_KEY);

// Batch submit up to 1000 URLs
const result = await indexer.pushBatch({
  urls: ['https://example.com/news/breaking', 'https://example.com/promo'],
  priority: 'high',
  targets: ['google', 'bing', 'yandex']
});

if (result.accepted === result.total) {
  console.log(`✅ Successfully queued ${result.accepted} URLs`);
} else {
  console.error(`⚠️ ${result.rejected} URLs failed validation`);
}
Rate Limits: Free tier allows 100 submissions/day. Professional tier: 10,000/day. Enterprise: Custom throughput with dedicated queues.