Contexts

Save the URL and API key of each Spica instance as a named context, and switch between local, staging and production.

A context is a named connection: the API URL of an instance and the API key the CLI uses for it. Every command that talks to an instance (fetch, plan, apply, dev, bucket orm, function orm, asset) uses the current context.

Create a key for the CLI

In the panel, open Access Management → API Keys, create a key, and attach the full-access policies of the modules you manage with the CLI: BucketFullAccess, FunctionFullAccess, PolicyFullAccess, EnvVarFullAccess and SecretFullAccess.

Add and select a context

Terminal
spica context set --name local --url http://localhost:4500/api --apikey <key>
spica context switch local
Text
Context has been changed to "local".

context set creates or updates a context. It doesn't select it, so run context switch afterwards.

OptionDescription
--nameName of the context.
--urlRequired. Public URL of the API, including /api when the API is served under that path.
-a, --apikeyRequired. The API key.

List and remove contexts

Terminal
spica context ls
Text
NAME        URL                             AUTHORIZATION
local       http://localhost:4500/api       * * * * * * * * * *
production  https://api.example.com/api     * * * * * * * * * *
Terminal
spica context remove production

Where contexts are stored

Contexts are saved in ~/.spicactx as JSON, with the key in plain text:

JSON
{"local": {"url": "http://localhost:4500/api", "authorization": "APIKEY 4yVYTsoIdIBETPoFn2jXoDY72i46L5I6"}}

Keep that file private. In CI, create the context from secrets at the start of the job:

Terminal
spica context set --name ci --url "$SPICA_URL" --apikey "$SPICA_API_KEY"
spica context switch ci

Guarding production

To avoid running plan or apply from the wrong folder, restrict them with a guard.