# Content Transfer API

The Content Transfer REST API allows SitecoreAI *Organization Admins* and *Organization Owners* to initiate, monitor, and finalize content transfer operations for migrating content data from one SitecoreAI environment (the *source*) to another (the *destination*). This enables efficient transfer of data between environments using chunked data streaming with encryption and compression support.

Each transfer operation includes at least one set of chunked data, where each chunk set represents a single data tree that was nominated for transfer. When an operation is complete, each of its chunk sets will be represented by a `.raif` file in the *destination* environment. To finish integrating the transferred data, use the separate [Item Transfer API](https://api-docs.sitecore.com/sai/item-transfer) to have the destination database consume the `.raif` files.

Note the following:
- To use this REST API, you must authorize your API requests.
- You must be an *Organization Admin* or *Organization Owner* to use this API. 
- Different endpoints in this API are called against different environments. You will switch between a *source* base URL and a *destination* base URL depending on the step in the workflow. Each endpoint description specifies which environment to use.

# Migrating content

Note: If you previously used the Package Designer for content migrations, [review the differences](https://doc.sitecore.com/sai/en/developers/sitecoreai/deploying-sitecoreai/migrating-content-between-sitecoreai-environments.html) between the designer and the modern API-based approach.

The Content Transfer API covers the *source* side of a content migration. After completing the steps in this API, use the [Item Transfer API](https://api-docs.sitecore.com/sai/item-transfer) to incorporate the transferred data into the destination database.

Before you begin, ensure you have the *Organization Admin* or *Organization Owner* role, and that you have determined your *source* and *destination* [base URLs](#section/base-url) and created a [JWT](#section/authorization) for each environment. 

## Create the transfer

In your *source* environment, [create a content transfer operation](#operation/ContentTransfer_CreateContentTransfer) using a unique transfer ID of your choice. For each item you want to transfer, specify the item path, whether to include descendants, and how to handle conflicts with existing items in the destination. Depending on how much data is being transferred, the operation creates one or more chunk sets.

Ensure that the item's parent chain already exists in the destination environment with the same item IDs. If it does not, the item will transfer successfully but will not appear in the content tree. To avoid this, transfer from a common ancestor using `Scope: ItemAndDescendants`, or transfer the parent items first.

## Wait for the transfer to be ready

[Poll the operation details](#operation/ContentTransfer_GetContentTransferStatus) in your *source* environment until `State` is `Completed`. If `State` is `Failed`, create a new transfer operation. For each chunk set in the response, record the `ChunkSetId` and `ChunkCount` because you'll need them to retrieve and save the chunks.

## Transfer chunks

For each chunk set, repeat the following for every chunk in the set:

1. In your *source* environment, [retrieve the chunk](#operation/ContentTransfer_GetChunkAsync). Make one request per chunk, using `chunkId` values `0` through `ChunkCount - 1`. Note the `IsMedia` value from the `Content-Disposition` response header because you'll need it when saving the chunk. You can retrieve multiple chunks in parallel.
2. In your *destination* environment, [save the chunk](#operation/ContentTransfer_SaveChunkAsync), forwarding the binary stream exactly as received without modification. You can save chunks in parallel with retrieval, but every chunk must be saved before completing the chunk set.

After all chunks in a set are saved, [complete the chunk set](#operation/ContentTransfer_CompleteChunkSetAsync) in your *destination* environment. This generates a `.raif` file and returns its name. Record the name for use in the Item Transfer API. Repeat for every chunk set in the operation.

## Delete the transfer

[Delete the transfer operation](#operation/ContentTransfer_DeleteContentTransfer) from your *source* environment to clean up all associated resources. You can do this immediately after downloading all chunks from the *source* environment. 

## Next steps

Proceed to the [Item Transfer API](https://api-docs.sitecore.com/sai/item-transfer) to incorporate the `.raif` files into the destination database.

# Base URL
In the base URL, replace `{host}` with your environment host name. You need two base URLs: one for your *source* environment (where you're migrating data from) and one for your *destination* environment (where you're migrating data to). You will switch between them as you follow the migration workflow.

Find the environment host name in SitecoreAI Deploy > **Projects** > your project > **Authoring environments** > your environment > **Details** > **Environment host name**.

Example environment host name: `your-environment.sitecorecloud.io`

# Authorization
To authorize your requests, use environment automation client credentials and generate a JSON Web Token (JWT).

Note: To create client credentials, you must be an [Organization Admin or Organization Owner](https://doc.sitecore.com/portal/en/developers/sitecore-cloud-portal/roles.html).

## Create an automation client
1. In the Sitecore Cloud Portal, open SitecoreAI Deploy.
2. Click **Credentials** > **Environment** > **Create credentials** > **Automation**.
3. Fill out the automation client details, then click **Create**.
4. Copy the client ID and the client secret because you won't be able to view them again in SitecoreAI Deploy. You'll use them to request a JWT.

## Request a JWT

Run the following cURL command to request a JWT. Replace the placeholder values with your client ID and client secret.
```curl
  curl -X POST 'https://auth.sitecorecloud.io/oauth/token' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'client_id={YOUR_CLIENT_ID}' \
  --data-urlencode 'client_secret={YOUR_CLIENT_SECRET}' \
  --data-urlencode 'grant_type=client_credentials' \
  --data-urlencode 'audience=https://api.sitecorecloud.io'
```

The JWT expires in 24 hours. If your requests unexpectedly return a response with status `403 Forbidden`, request a new JWT by repeating this `POST` request.

We recommend that you cache the JWT for 24 hours to avoid repeating this `POST` request while the JWT is still valid.

## Include the JWT in the request header

You can now start making REST API requests. You must include the JWT in the request header of every request. For example:
```curl
  curl -X GET '{YOUR_BASE_URL}/...' \
  -H 'Authorization: Bearer {YOUR_JWT}' \
  -H 'Accept: application/json'
```


Version: v1.0
License: closed source

Metadata:
  - product: SitecoreAI

## Servers

```
https://{host}
```

Variables:
- `host`: The environment host name.
  Default: "your-environment.sitecorecloud.io"

## Security

### bearerAuth

Type: http
Scheme: bearer
Bearer Format: JWT

## Download OpenAPI description

 - [Content Transfer API](https://api-docs.sitecore.com/_bundle/sai/content-transfer-api/index.yaml)

## Content transfer

 - [POST /sitecore/api/content/transfer/v1/transfers](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_createcontenttransfer.md): Creates a new operation to transfer one or more specified items from your *source* environment. You must specify a unique ID of your choice for the transfer, which you'll use later in the transfer wor
 - [GET /sitecore/api/content/transfer/v1/transfers/{transferId}/status](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_getcontenttransferstatus.md): Retrieves information about a specific content transfer operation, including its current status and a list of the chunk sets included. Make this request in your *source* environment. For each chunk se
 - [GET /sitecore/api/content/transfer/v1/transfers/{transferId}/chunksets/{chunksetId}/chunks/{chunkId}](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_getchunkasync.md): Use this endpoint to retrieve the binary data for a single chunk. Make one request per chunk per chunk set, using `chunkId` values `0` through `ChunkCount - 1` (where `ChunkCount` is obtained from the
 - [PUT /sitecore/api/content/transfer/v1/transfers/{transferId}/chunksets/{chunksetId}/chunks/{chunkId}](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_savechunkasync.md): For each chunk of content in a set within a transfer operation, use this endpoint to copy the chunk to the *destination* environment after using the corresponding [Retrieve a specific chunk of transfe
 - [POST /sitecore/api/content/transfer/v1/transfers/{transferId}/chunksets/{chunksetId}/complete](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_completechunksetasync.md): After all the chunk data in a particular chunk set has been copied to the *destination* environment, use this endpoint to generate a `.raif` file containing the chunk set data on that environment. The
 - [DELETE /sitecore/api/content/transfer/v1/transfers/{transferId}](https://api-docs.sitecore.com/sai/content-transfer-api/content-transfer/contenttransfer_deletecontenttransfer.md): If you no longer need a particular content transfer operation—either because you've finished converting the contents of its chunk sets into `.raif` files, or because the transfer was unsuccessful or c
