Automate management of your Upsun projects
Anything you can do, API can do better!An interesting fact about the Upsun hosting and deployment system, is that all the important actions are done via an API (Abstract Programming Interface) that you also have access to. The changes you make and the actions that you invoke through the web console can also be triggered programmatically by authenticated HTTP requests. This means that anything you can do in the Web UI console, you can also automate or trigger remotely!
Note:Some documentation links may still refer to “Platform.sh”, the
platform CLI tool, and PLATFORM_* named environment variables.
In most cases, you can imagine that the upsun CLI tool acts identically to the platform CLI tool, so you may read the docs as if they are interchangeable.The CLI is mostly an API wrapper
We provide theupsun CLI to make this easier ( not least because the authentication stuff is annoying to do by hand ) but all that usually does is make an API request on your behalf.
You can see that in action by using UPSUN_CLI_DEBUG (a flag that makes the upsun CLI tool more verbose). That will log the actual request being made to the API.
API requests are authenticated
The authentication that is used for API access controls what you can do based on the projects and permissions that are assigned to your user account.Note that it is recommended to create a “machine user” with its own token if you are intending to delegate tasks to an environment where the token or keys may be shared or visible to others.
- When you use the web console, a temporary certificate has been given to your browser when you authenticated -
usually when you went through
auth.upsun.comto do your login. This allows the web console in your browser to talk directly toapi.upsun.comfor you. - When you use the CLI, it may require you to “Log in via a browser” window in the same way. and a temporary certificate becomes available to your shell environment once you are “Successfully logged in”.
- You can also avoid logging in each time you use the CLI by authenticating using an API token
This is more convenient if you use the CLI a lot.
- To create an API token, go to the “API Tokens” section of the “Account Settings” tab on the Console, and make that available as a variable named
UPSUN_API_TOKENin your shell environment.
- To create an API token, go to the “API Tokens” section of the “Account Settings” tab on the Console, and make that available as a variable named
- For making requests directly via the API, or for automation and delegation of tasks, you would also use an API token in this same way.
- To use the token, the
UPSUN_API_TOKENvariable must be set in the environment for the CLI, or passed explicitly in an API request header. - You can experiment with semi-raw API requests but still use the CLI to manage authentication via the
upsun api:curlcommand. For inline documentation onapi:curlenterupsun help api:curlin your terminal.
- To use the token, the
Ways of interacting with the API
It’s possible to make raw http requests directly to the API endpoint, but it’s usually better to use a wrapper of some sort.Use upsun project:curl
The CLI has an embedded utility that provides direct access to the http requests, and handles the project selection and authentication for you.
See upsun help project:curl
platform environment:list, but the JSON response may be more useful for automation.
project:curl is a shortcut for api:curl that pre-selects the project for you, and is easier to use for normal operations.
They both really are just very thin wrappers around the documented endpoints that format the curl command options for you.Use curl directly
Use postman
The API support documentation also provides an OpenAPI specification which can be imported into tools like Postman for inspection and testing.
- Install the Postman API explorer
- “Import” the API schema from
https://api.upsun.com/docs/openapispec-platformsh.json - Update the “Variables” to set the
baseUrlto{{schemes}}://api.upsun.com - Update the “Authorization” section to set “Auth Type” type “Bearer Token”
- Generate a token with
upsun auth:tokenand paste that into the “Token” field - Test a request by running the simple “GET” “me” operation
Use automation to manage your project
With access to this toolset now, you may be able to see some possibilities for management.- As Upsun resources are charged for pro-rata (usage based), you could cut the RAM and CPU costs for all your preview environments in half by automating a
crontask them to “pause” every night and weekend. - You can upscale and downscale the resources in realtime to match demand and traffic on the website.