Contentful Batch Utilities Library

11.0.0 · active · verified Sun Apr 19

This library provides core modules and shared methods utilized by Contentful's batch utility CLI tools, specifically for `contentful-import` and `contentful-export`. It offers low-level programmatic access to operations such as getting source content from a space, pushing changes (including deletions, creations, and publishing/unpublishing) to a destination space, and various content transformations. It also includes utilities for creating Contentful clients and error handling. The current stable version is 11.0.0. The project uses `semantic-release`, indicating a release cadence of frequent patch/minor versions and major versions only for breaking changes. Its key differentiator lies in providing granular, batch-oriented control over Contentful space manipulation, acting as a foundational layer for more complex data migration and synchronization workflows between Contentful environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Contentful clients, fetch all content from a source space, and then push that content (or transformed content) back to a target space using `contentful-batch-libs`.

import { createClients, getSourceSpace, pushToSpace } from 'contentful-batch-libs';

const SPACE_ID = process.env.CONTENTFUL_SOURCE_SPACE_ID ?? '';
const MANAGEMENT_TOKEN = process.env.CONTENTFUL_MANAGEMENT_TOKEN ?? '';
const DELIVERY_TOKEN = process.env.CONTENTFUL_DELIVERY_TOKEN ?? '';
const ENVIRONMENT_ID = process.env.CONTENTFUL_ENVIRONMENT_ID ?? 'master';

if (!SPACE_ID || !MANAGEMENT_TOKEN || !DELIVERY_TOKEN) {
  console.error('Environment variables CONTENTFUL_SOURCE_SPACE_ID, CONTENTFUL_MANAGEMENT_TOKEN, and CONTENTFUL_DELIVERY_TOKEN must be set.');
  process.exit(1);
}

async function runBatchOperation() {
  try {
    // 1. Create Contentful client instances
    const { managementClient, deliveryClient } = createClients({
      spaceId: SPACE_ID,
      managementToken: MANAGEMENT_TOKEN,
      accessToken: DELIVERY_TOKEN,
      environmentId: ENVIRONMENT_ID,
    });

    console.log('Contentful clients created successfully.');

    // 2. Get content from the source space
    console.log(`Fetching content from space '${SPACE_ID}'...`);
    const sourceContent = await getSourceSpace({
      managementClient,
      deliveryClient,
      spaceId: SPACE_ID,
      environmentId: ENVIRONMENT_ID,
      // You might want to specify content types, entries, assets, etc. to fetch
      // For example: query: { 'content_type': 'blogPost' }
    });

    console.log(`Fetched ${sourceContent.entries.length} entries and ${sourceContent.assets.length} assets.`);

    // 3. (Optional) Transform content here if needed
    // const transformedContent = transformSpace(sourceContent, /* transformers */);

    // 4. Push transformed or original content to a destination space (example uses the same space ID for simplicity)
    // In a real scenario, you'd likely have a separate destination space ID and clients.
    console.log(`Pushing content back to space '${SPACE_ID}'...`);
    await pushToSpace({
      sourceContent,
      managementClient,
      spaceId: SPACE_ID,
      environmentId: ENVIRONMENT_ID,
      // Optional: publish: false, contentModelOnly: true, etc.
    });

    console.log('Batch operation completed successfully.');
  } catch (error) {
    console.error('An error occurred during the batch operation:', error);
    process.exit(1);
  }
}

runBatchOperation();

view raw JSON →