Spacelift
PrivacyT&Cs
  • 👋Hello, Spacelift!
  • 🚀Getting Started
  • 🌠Main concepts
    • Stack
      • Creating a stack
      • Stack settings
      • Organizing stacks
      • Stack locking
      • Drift detection
    • Configuration
      • Environment
      • Context
      • Runtime configuration
        • YAML reference
    • Run
      • Task
      • Proposed run (preview)
      • Tracked run (deployment)
      • Module test case
      • User-Provided Metadata
      • Run Promotion
      • Pull Request Comments
    • Policy
      • Login policy
      • Access policy
      • Approval policy
      • Initialization policy
      • Plan policy
      • Push policy
      • Task policy
      • Trigger policy
    • Resources
    • Worker pools
    • VCS Agent Pools
  • 🛰️Platforms
    • Terraform
      • Module registry
      • External modules
      • Provider
      • State management
      • Terragrunt
      • Version management
      • Handling .tfvars
      • CLI Configuration
      • Cost Estimation
      • Resource Sanitization
      • Storing Complex Variables
      • Debugging Guide
    • Pulumi
      • Getting started
        • C#
        • Go
        • JavaScript
        • Python
      • State management
      • Version management
    • CloudFormation
      • Getting Started
      • Reference
      • Integrating with SAM
      • Integrating with the serverless framework
    • Kubernetes
      • Getting Started
      • Authenticating
      • Custom Resources
      • Helm
      • Kustomize
  • ⚙️Integrations
    • Audit trail
    • Cloud Integrations
      • Amazon Web Services (AWS)
      • Microsoft Azure
      • Google Cloud Platform (GCP)
    • Source Control
      • GitHub
      • GitLab
      • Azure DevOps
      • Bitbucket Cloud
      • Bitbucket Datacenter/Server
    • Docker
    • GraphQL API
    • Single sign-on
      • GitLab OIDC Setup Guide
      • Okta OIDC Setup Guide
      • OneLogin OIDC Setup Guide
      • Azure AD OIDC Setup Guide
      • AWS IAM Identity SAML 2.0 Setup Guide
    • Slack
    • Webhooks
  • 📖Product
    • Privacy
    • Security
    • Support
      • Statement of Support
    • Disaster Continuity
    • Billing
      • Stripe
      • AWS Marketplace
    • Terms and conditions
    • Refund Policy
  • Cookie Policy
Powered by GitBook
On this page
  • Spacelift GraphQL API Usage Demo
  • API Usage
  • SpaceCTL CLI > Token
  • Spacelift API Key > Token
  • Personal GitHub Token > Token
  • Viewing the GraphQL Schema
  • Insomnia Example
  • GraphiQL Example

Was this helpful?

  1. Integrations

GraphQL API

Describes how to authenticate and use the Spacelift GraphQL API.

PreviousDockerNextSingle sign-on

Last updated 2 years ago

Was this helpful?

Spacelift GraphQL API Usage Demo

The below guide walks through an example of generating your Spacelift token and using it to communicate with Spacelift.

Pre-requisites:

  • Insomnia downloaded and installed

  • Spacelift account access

  • CLI tool downloaded and installed

API Usage

For example, if your Spacelift account is called "example" you would be able to access your GraphQL by sending POST requests to: https://example.app.spacelift.io/graphql

All requests need to be authenticated using a bearer token, which we will discuss in more detail below.

In order to use the API, you will need a bearer token to authenticate your requests. There are currently three ways of obtaining this token:

SpaceCTL CLI > Token

Steps:

  1. Authenticate to your Spacelift account using spacectl profile login

  2. Once authenticated, run spacectl profile export-token to receive the bearer token needed for future GraphQL queries/mutations.

Spacelift API Key > Token

Spacelift supports creating and managing machine users with programmatic access to the Spacelift GraphQL API. These "machine users" are called API Keys and can be created by Spacelift admins through the Settings panel.

In order to create a new API Key, please navigate to the API Keys section of the Account Settings panel in your account.

Click API Keys.

Without further ado, let's create a non-administrative API key with virtual membership in two teams: Developers and DevOps:

The config file looks something like this:

Please use the following API secret when communicating with Spacelift
programmatically:

SECRET_VALUE40ffc46887297384892384789239

Please add this snippet to your .terraformrc file if you want to use this API
key to access Spacelift-hosted Terraform modules outside of Spacelift:

credentials "spacelift.io" {
  token = "TOKEN_VALUEQwZmZjNDY4ODdiMjI2ZWE4NDhjMWQwNWZiMWE5MGU4NWMwZTFlY2Q4NDAxMGI2ZjA2NzkwMmI1YmVlMWNmMGE"
}

Make sure you persist this data somewhere on your end - we don't store the token and it cannot be retrieved or recreated afterwards.

Using the API key

In order to use your newly generated API key in a program, you will first need to exchange it for a JWT token using a GraphQL mutation:

Steps:

  1. Create a Spacelift API Key in your account, take note of the API Key ID (found next to the name of your API Key), and the API Key Secret (found within the file downloaded upon creation of the API Key).

Request Details:

POST to https://example.app.spacelift.io/graphql

Replace "example" with the name of your Spacelift account.

Query:

mutation GetSpaceliftToken($keyId: ID!, $keySecret: String!) {
  apiKeyUser(id: $keyId, secret: $keySecret) {
    jwt
  }
}

You'll need to pass in the values of keyId and keySecret as input query variables for the above example query to work. In your API Client you should see a section called "Query Variables" where you can pass in an input variables for your query.

Query Variables Input:

{
    "keyId": "PASTE_API_KEY_ID_HERE",
    "keySecret": "PASTE_API_KEY_SECRET_HERE"
}

Assuming all went well, the result of the above query will return your JWT token, which you will now be able to use to authenticate for other queries.

Note that the key ID is the alphanumeric identifier shown in the GUI in fixed-width font next to the key name. The key secret can be found in the file that gets automatically generated when the API key is created.

The received JWT is valid for an hour, so if you're accessing Spacelift API from a long-running process you will need to make sure that the key is recreated every time it expires. In order to help with that, you can retrieve the validUntil field (Unix timestamp of the expiration, in seconds) of the apiKeyUser along with the raw JWT.

API keys are in fact virtual users and are billed like regular users, too. Thus, each API key used (exchanged to a token) during any given billing cycle counts against the total number of users.

Personal GitHub Token > Token

Steps:

Request Details:

POST to https://example.app.spacelift.io/graphql

Replace "example" with the name of your Spacelift account.

Query:

mutation GetSpaceliftToken($token: String!) {
  oauthUser(token: $token) {
    jwt
  }
}

You'll need to pass in token as a query variable for the above example query to work. When making a GraphQL query with your favorite API Client, you should see a section called GraphQL variables where you can pass in an input.

GraphQL Variables Input:

{
    "token": "PASTE-TOKEN-VALUE-HERE"
}

Viewing the GraphQL Schema

Please replace the URL in the below examples with the one pointing to your Spacelift account.

Insomnia Example

GraphiQL Example

Input your GraphQL Endpoint for your Spacelift Account.

Use the Documentation Explorer within GraphiQL

Spacelift provides a GraphQL API for you to control your Spacelift account programmatically and/or through an API Client if you choose to do so. A smaller subset of this API is also used by the Spacelift , as well as the Spacelift CLI (). The API can be accessed at the /graphql endpoint of your account using POST HTTP method.

(easiest method)

One approach to generating this token is using the Spacelift CLI. We consider this the easiest method, as the heavy lifting to obtain the token is done for you.

Follow the instructions on the spacectl to install the CLI on your machine.

The API key creation form will allow you to specify an arbitrary key name, along with the Admin setting and the list of teams. If the key is given admin privileges, it has full access to the Spacelift API and won't be subject to .

For non-administrative keys, you may want to add a virtual list of teams that the key should "belong to" so that existing access policies based on or can work with your API keys just as they do with regular users.

Once you click the Add Key button, the API Key will be generated and a file will be automatically downloaded. The file contains the API token in two forms - one to be used with our API, and the other one as a .terraformrc snippet to access your outside of Spacelift:

Using your favorite API Client (e.g. or ). Make a GraphQL query to your account's GraphQL endpoint (example below).

This option is only available to those using GitHub as their identity provider. If you have enabled any other on your account, this method will not work. If this applies to you, you will need to use the method instead.

Using a GitHub Account that has access to your Spacelift account, . Copy the value of this token to a secure location, as you'll need it in the next step.

Using your favorite API Client (e.g. or ). Make a GraphQL POST request to your account's GraphQL endpoint (example below).

Assuming all went well, the result of the above query will return your JWT bearer token, which you will now be able to use to authenticate other queries. Once acquired, ensure you use this bearer token in your requests. If you want to access the API reliably in an automated way, we suggest using the approach as Spacelift tokens expire after 1 hour.

Our GraphQL schema is self-documenting. The best way to view the latest documentation is using a dedicated GraphQL client like or . Note: As of the writing of these examples, the latest version of Postman does not currently support viewing GraphQL Schemas from a URL, but does support autocompletion.

⚙️
Terraform provider
spacectl
SpaceCTL CLI > Token
Spacelift API Key > Token
Personal GitHub Token > Token
spacectl
GitHub repository
access policies
GitHub teams
SAML assertions
private modules
Insomnia
GraphiQL
Single sign-on methods
Spacelift API Key > Token
create a GitHub Personal Access Token
Insomnia
GraphiQL
Spacelift API Key > JWT Token
Insomnia
GraphiQL
Spacectl
Click Settings.
Click API Keys.
Create and Configure your new API Key.
Download your API Key.
Example viewing GraphQL documentation using Insomnia.
Click Docs.