Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added section for API usage with python #292

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/integrations/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,62 @@ The below guide walks through an example of generating your Spacelift token with
<!-- markdownlint-disable-next-line MD033 -->
<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/1cefc584b1bc41d7bc75d767afaf3916" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>

## API Usage with Python

Below is an example of interacting with the api using python. There are three Environment Variables you will need to set.
jacobjSL marked this conversation as resolved.
Show resolved Hide resolved

- `SPACELIFT_KEY_ID`- This will be the Id of the [API Key](https://docs.spacelift.io/integrations/api.html#spacelift-api-key-token) you created above. It should be a 26-character ULID.
jacobjSL marked this conversation as resolved.
Show resolved Hide resolved
- `SPACELIFT_KEY_SECRET` - This will be found in the file that was downloaded when you created the [API Key](https://docs.spacelift.io/integrations/api.html#spacelift-api-key-token).
jacobjSL marked this conversation as resolved.
Show resolved Hide resolved
- `SPACELIFT_BASE_URL` - This will be be the URL of your Spacelift account. For example, `https://my-account.app.spacelift.io/graphql`
jacobjSL marked this conversation as resolved.
Show resolved Hide resolved

The Python code is below but you can also visit the [GitHub Repo](https://github.com/spacelift-io/spacelift-api-examples) for other use cases and examples.
jacobjSL marked this conversation as resolved.
Show resolved Hide resolved

??? note "Click here to expand"

{% raw %}

```python
jmfontaine marked this conversation as resolved.
Show resolved Hide resolved
{
import sys
import requests
import json
import os

# Set variables
keyId = os.environ.get('SPACELIFT_KEY_ID')
keySecret = os.environ.get('SPACELIFT_KEY_SECRET')
baseURL = os.environ.get('SPACELIFT_BASE_URL')
mutatationVariables = {'keyId': keyId, 'keySecret': keySecret}

#The GraphQL mutation to get the Bearer Token
mutation = """mutation GetSpaceliftToken($keyId: ID!, $keySecret: String!) {apiKeyUser(id: $keyId, secret: $keySecret) {jwt}}"""

# ---The API call---
# By default this will be a basic Stack query
# unless you pass in a custom request as an argument
query = sys.argv[1] if len(sys.argv) > 1 else """{stacks {id name space administrative state}}"""

# function to create the jwt(spacelift token) for the header
def getSpaceliftToken():
request = requests.post(baseURL, json={'query': mutation, 'variables': mutatationVariables})
response = request.json()
token = response['data']['apiKeyUser']['jwt']
return token

# function to make the API call
def runQuery(query):
request = requests.post(baseURL, json={'query': query}, headers=headers)
print(json.dumps(request.json(), indent=4))

# Execute the API call
jwt = getSpaceliftToken()
headers = {"Authorization": f"Bearer {jwt}"}
runQuery(query)
}
```

{% endraw %}

## Authenticating with the GraphQL API

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`
Expand Down