Algolia API powershell

For future reference saving samples of how to work with Algolia from powershell

# https://dashboard.algolia.com/account/api-keys/all
$APPLICATION_ID = $env:ALGOLIA_APPLICATION_ID
$SEARCH_ONLY_API_KEY = $env:SEARCH_ONLY_API_KEY
$ADMIN_API_KEY = $env:ADMIN_API_KEY

# https://www.algolia.com/doc/rest-api/search/#hosts
$ADMIN_DOMAIN = "$APPLICATION_ID.algolia.net"
$SEARCH_DOMAIN = "$APPLICATION_ID-dsn.algolia.net"

$adminHeaders = @{
    "X-Algolia-API-Key" = "$ADMIN_API_KEY"
    "X-Algolia-Application-Id" = "$APPLICATION_ID"
    "Content-Type" = "application/json; charset=utf-8"
}

$searchHeaders = @{
    "X-Algolia-API-Key" = "$SEARCH_ONLY_API_KEY"
    "X-Algolia-Application-Id" = "$APPLICATION_ID"
    "Content-Type" = "application/json; charset=utf-8"
}

# https://www.algolia.com/doc/rest-api/search/#list-indices
Invoke-RestMethod -Method Get "https://$ADMIN_DOMAIN/1/indexes" -Headers $adminHeaders | Select-Object -ExpandProperty items

# https://www.algolia.com/doc/rest-api/search/#delete-index
Invoke-RestMethod -Method Get "https://$ADMIN_DOMAIN/1/indexes/talks" -Headers $adminHeaders

# https://www.algolia.com/doc/api-client/methods/manage-indices/#create-an-index
# Algolia creates a new index when you add a new object or add a setting to an empty index for the first time

# ----------------------------------------------------------

# https://www.algolia.com/doc/rest-api/search/#add-or-replace-object
Invoke-RestMethod -Method Post "https://$ADMIN_DOMAIN/1/indexes/talks" -Headers $adminHeaders -Body (ConvertTo-Json -Depth 100 -InputObject @{
    objectID = "1" # optional, auto generated
    title = "My Talk"
    description = "My Description"
})

# https://www.algolia.com/doc/rest-api/search/#get-object
# GET, DELETE, PUT
Invoke-RestMethod -Method Get "https://$SEARCH_DOMAIN/1/indexes/talks/1" -Headers $searchHeaders

# https://www.algolia.com/doc/rest-api/search/#search-index-get
Invoke-RestMethod -Method Get "https://$SEARCH_DOMAIN/1/indexes/talks?query=my" -Headers $searchHeaders | Select-Object -ExpandProperty hits

# https://www.algolia.com/doc/rest-api/search/#set-settings
# https://www.algolia.com/doc/api-reference/settings-api-parameters/
Invoke-RestMethod -Method Put "https://$ADMIN_DOMAIN/1/indexes/talks/settings" -Headers $adminHeaders -Body (ConvertTo-Json -Depth 100 -InputObject @{
    searchableAttributes = @("title", "description", "transcript")
    ignorePlurals = $true # Treats singular, plurals, and other forms of declensions as matching terms.
    queryLanguages = @("en", "ru", "uk")
    indexLanguages = @("en", "ru", "uk")
})