# 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

API endpoints for content transfer operations.

### Create a content transfer operation

 - [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 workflow.

Make this request in your source environment.

{% admonition type="warning" name="Warning" %}If you reuse a previous transfer ID, the transfer operation associated with that ID will be overwritten (if it was not previously deleted using this API).

By default, a transfer will only include single items based on the paths you specify, and it will replace any duplicate items found in the destination environment. You can override these settings to include descendants or choose a different merge strategy as needed.

### Retrieve details of a content transfer operation

 - [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 set in the specified operation, the response contains:
- ChunkSetId: the unique identifier of the chunk set, used as a path parameter in subsequent requests.
- ChunkCount: the number of chunks in the set. Make one Retrieve a specific chunk of transfer data request and one Save a data chunk to the destination environment request per chunk, using chunkId values 0 through ChunkCount - 1.
- TotalItemCount: the total number of content and media items in the chunk set.

### Retrieve a specific chunk of transfer data

 - [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 Retrieve details of a content transfer operation response).

Make this request in your source environment.

The response body contains a binary stream of serialized item data. The first chunk in a given set also includes a header. Every chunk you retrieve must also be saved to the destination environment using the Save a data chunk to the destination environment endpoint. You can retrieve and save chunks in parallel, but all chunks in a set must be saved before you can complete that chunk set.

Depending on whether the chunk represents a media item or a content item, the data will either be compressed or encrypted, and the returned value of IsMedia will either be true or false:
- Media data (IsMedia = true) is compressed.
- Content data (IsMedia = false) is encrypted.

Content description metadata is exposed via Content-Disposition header parameters:
- ItemsProcessed: number of successfully processed items written into the stream
- ItemsSkipped: number of items in the chunk that were skipped (failed / filtered)
- IsMedia: indicates whether the returned payload was compressed (true) or encrypted (false).

### Save a data chunk to the destination environment

 - [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 transfer data endpoint, where you obtained a binary stream representing the chunk data.

Make this request in your destination environment.

Set the request header Content-Type: application/octet-stream.

{% admonition type="important" name="Important" %}The request body for this endpoint must be the raw binary stream obtained from the response of the paired endpoint.

Do not alter, wrap, re-encode or chunk the stream; forward it exactly as received:
- For media chunks, the client must send the compressed bytes as-is, with no decompression.
- For content (non-media) chunks, the client must send the encrypted bytes as-is, with no decryption.

After retrieving and transferring all the chunks in a particular set, use the Complete a chunk set endpoint to generate a .raif file containing that chunk set's data on the destination environment.

### Complete a chunk set

 - [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 name of this new file is returned in the body of a successful call to this endpoint.

Make this request in your destination environment.

Make sure to complete every chunk set.

To incorporate the .raif file data into the destination environment's database, use the separate Item Transfer API.

### Delete a content transfer and clean up associated resources

 - [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 cancelled—use this endpoint to delete the operation and all associated resources that are no longer required.

Make this request in your source environment.

