Example: Create a service end to end
This documentation is part of the Use the CLI guide. View the full guide here: Install and use the Stackhero CLI to manage your stacks and services from the command line, scripts, CI, and AI agents.
Here is a sample script that shows how you can create a stack, add a service, wait for it to be ready, and retrieve its credentials. This example is fully non-interactive and uses the STACKHERO_TOKEN environment variable.
#!/bin/bash
set -e
export STACKHERO_TOKEN="usr-xxxxxx:your-token"
serviceStore="directus" # A service store name. See: `stackhero services-store-list`
instance="1GB" # An instance (size) name. See: `stackhero instances-store-list --service-store=directus`
region="europe" # A region name. See: `stackhero regions-list`
# Create a stack (uses your only organization, add `--organization` if you have several)
stackId=$(stackhero --format=script stack-create \
--name="My stack")
echo "Stack: ${stackId}"
# Add a service (names are resolved automatically, and the stack ID above also works)
serviceId=$(stackhero --format=script service-add \
--stack="My stack" \
--service-store="${serviceStore}" \
--instance="${instance}" \
--region="${region}")
echo "Service: ${serviceId}"
# Wait for the service to be running
stackhero service-wait-for --service="${serviceId}"
# Retrieve credentials and configuration
stackhero service-configuration-get --service="${serviceId}" --format=json
Each service documentation page includes a ready-to-run Automate with the CLI guide tailored to that service, so you have everything you need to get started.