SitecoreAI's new Content Types, Components and Content Items APIs let you create components and their content with a script instead of clicking through the Content Editor. This post shows how to use them from PowerShell, with a working sample and the use cases they open up.
The three APIs
The three APIs are used in this order.
- Content Types API: Creates the content type, which defines the fields, such as Heading and Body.
- Components API: Creates the component and links it to the content type.
- Content Items API: Creates content items filled in with real values.
The APIs identify everything by ID, including fields. For example, to fill in Heading, you send the Heading field's ID.
Before you start
Create credentials in SitecoreAI Deploy by opening Credentials, then the Environment tab, and choosing Automation. Organization credentials won't work because every call made with them returns 401 Unauthorized.
Then request a token, which lasts 24 hours, using the following code.
$Token = (Invoke-RestMethod -Method Post -Uri "https://auth.sitecorecloud.io/oauth/token" `
-ContentType "application/x-www-form-urlencoded" -Body @{
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
grant_type = "client_credentials"
audience = "https://api.sitecorecloud.io"
}).access_token
Create a component
The following script creates a content type, a component linked to it, and a content item for the component to display.
# ---------------- Settings ----------------
$ClientId = "YOUR_CLIENT_ID"
$ClientSecret = "YOUR_CLIENT_SECRET"
$Name = "Promo Banner"
$CategoryName = "Page Content"
$ParentId = "YOUR_DATA_FOLDER_ITEM_ID" # where the content item goes
$Fields = @(
@{ name = "Heading"; type = "Single-Line Text"; sortOrder = 100 },
@{ name = "Body"; type = "Rich Text"; sortOrder = 200 }
)
$Values = @{ Heading = "Summer Sale - 30% off"; Body = "<p>This week only.</p>" }
# -------------------------------------------
$Base = "https://edge-platform.sitecorecloud.io/authoring"
# 1. Token
$Token = (Invoke-RestMethod -Method Post -Uri "https://auth.sitecorecloud.io/oauth/token" `
-ContentType "application/x-www-form-urlencoded" -Body @{
client_id = $ClientId; client_secret = $ClientSecret
grant_type = "client_credentials"; audience = "https://api.sitecorecloud.io"
}).access_token
function Invoke-Sitecore($Method, $Path, $Body) {
$p = @{ Method = $Method; Uri = "$Base$Path"; Headers = @{ Authorization = "Bearer $Token" } }
if ($Body) { $p.Body = ($Body | ConvertTo-Json -Depth 10); $p.ContentType = "application/json" }
Invoke-RestMethod @p
}
# List responses can be a plain array or wrapped (e.g. { "items": [...] })
function Get-List($r) {
if ($r -is [array]) { return $r }
foreach ($p in $r.PSObject.Properties) { if ($p.Value -is [array]) { return $p.Value } }
@($r)
}
# 2. Content type (the blueprint)
$ct = Invoke-Sitecore POST "/api/v1/content-types?environmentId=main" @{
name = $Name
fieldGroups = @( @{ name = "Content"; sortOrder = 100; fields = $Fields } )
}
$fieldIds = @{}
$ct.fieldGroups | ForEach-Object { $_.fields } | ForEach-Object { $fieldIds[$_.name] = $_.id }
Write-Host "Content type: $($ct.id)"
# 3. Component, linked to the content type
$cats = Get-List (Invoke-Sitecore GET "/api/v1/component-categories?environmentId=main")
$cat = $cats | Where-Object { $_.name -eq $CategoryName -or $_.displayName -eq $CategoryName } | Select-Object -First 1
$comp = Invoke-Sitecore POST "/api/v1/components?environmentId=main" @{
name = $Name; displayName = $Name
category = @{ id = $cat.id; name = $CategoryName }
modelId = $ct.id
}
Write-Host "Component: $($comp.id)"
# 4. Content item, using field IDs (not names)
$itemFields = @($Values.Keys | ForEach-Object { @{ id = $fieldIds[$_]; value = @{ text = $Values[$_] } } })
$item = Invoke-Sitecore POST "/api/v1/content-items?environmentId=main" @{
name = "Summer Sale Promo"; parentId = $ParentId
templateId = $ct.id; language = "en"; fields = $itemFields
}
Write-Host "Content item: $($item.path)"To run it more than once, check whether each part already exists before creating it.
Removing content
Content items can be deleted through the API. Components can't, because the Components API only deletes drafts, and no API removes a component from a page. Do both in SitecoreAI instead.
$Base = "https://edge-platform.sitecorecloud.io/authoring"
Invoke-RestMethod -Method Delete -Headers @{ Authorization = "Bearer $Token" } `
-Uri "$Base/api/v1/content-items/YOUR_ITEM_ID?environmentId=main"The item goes to the Recycle Bin. To delete it permanently instead, add &permanently=true to the address.
Use cases
- Component scaffolding. Create a content type, component and sample item in one run instead of clicking through the Content Editor.
- Environment consistency. Create the same components in dev, QA and production without differences creeping in.
- Bulk updates and cleanup. Change a field across many items, or remove test items, in one run.
- Component and draft cleanup. Delete component drafts that were never activated. Only drafts can be deleted through the Components API. An actual component can't be deleted directly, so remove it in SitecoreAI.

