GraphQL API
This reference describes the Instruqt GraphQL API and helps you get started.

What is the GraphQL API?

The Instruqt GraphQL API lets you exchange data between your systems and the Instruqt platform. As the name suggests, the API is based on GraphQL, a query language for APIs. GraphQL lets you query and update data with queries and mutations in JSON format. This means that you can do the following actions with the Instruqt GraphQL API:
  • Query for information about Instruqt artifacts like tracks and challenges.
  • Add Instruqt artifacts like track invites or team members.
  • Update Instruqt artifacts, including deleting them.
The query response is in JSON format, and the following is an example of a GraphQL query that lists all your tracks by id, slug and title:
GraphQL
1
query {
2
tracks(organizationSlug: \"instruqt\") {
3
id
4
slug
5
title
6
}
7
}
Copied!
Which gives a response like this:
JSON
1
{
2
"data": {
3
"tracks": [
4
{
5
"id": "amy5xhuaxukc",
6
"slug": "aws-cloud-account",
7
"title": "AWS Cloud Account"
8
},
9
{
10
"id": "jrwu2g1tymzn",
11
"slug": "azure-subscription",
12
"title": "Azure subscription"
13
},
14
{
15
"id": "4ttuxz0fgyf1",
16
"slug": "gcp-project",
17
"title": "GCP project"
18
},
19
{
20
"id": "vnt0zc9owl5c",
21
"slug": "kubernetes",
22
"title": "Kubernetes"
23
}
24
]
25
}
26
}
Copied!

Why would you use the GraphQL API?

The API is based on GraphQL, so you can form your own queries and are not bound to predefined requests and responses with unnecessary data. Furthermore, you can access the API from any modern programming language and software like Excel and Zapier.
Here are some possible scenarios:
  • You can extract a list with participant IDs from Instruqt.
  • You can add an Excel list with new team members to Instruqt.
  • You can add a JSON file with track invites to Instruqt.

How to use the GraphQL API

Address the API Endpoint

You address all your GraphQL queries and mutations as HTTP POST requests to an API Endpoint. The GraphQL API Endpoint for Instruqt is:
Method
Endpoint
​
​
https://play.instruqt.com/graphql

Create your API key for authorization

Instruqt GraphQL API requires your queries and mutations to be authorized by an API key. Take the following steps to create your API key:
🌐 Web UI
  1. 1.
    Open your browser and go to play.instruqt.com. ↳ Instruqt shows your Content page.
  2. 2.
    Click Team settings, followed by API keys. ↳ Instruqt shows the Manage API keys page.
  3. 3.
    Click Generate API Key. ↳ The API Key field now shows the generated API key.
  4. 4.
    Click Copy to clipboard. ↳ You can apply the key now. You do that by passing the key to the HTTP request Authorization: Bearer parameter of your GraphQL query request.

Explore the API

Before you create queries and mutations, you should get familiar with the available queries and mutations and find the ones you need for your taskβ€”for example, list all tracks. You can explore the queries and mutations with the following resources:
  • The GraphQL Playground desktop app
  • The API Reference
Instruqt recommends GraphQL Playground, a GraphQL IDE that lets you explore an API and allows you to experiment with queries and mutations. You can download GraphQL and install it as a desktop app. Follow the installation instructions, start GraphQL Playground, and then:
GraphQL Playground
  1. 1.
    Add the following HTTP header:
    1
    {"Authorization": "Bearer YOUR_API_KEY"}
    Copied!
    ⇨ Replace YOUR_API_KEY with the copied key.
  2. 2.
    In the Endpoint field, enter https://play.instruqt.com/graphql.
  3. 3.
    Click the Docs tab. ↳ A list with QUERIES and MUTATIONS opens.
  4. 4.
    Browse through the queries and mutations to get familiar with themβ€”for example, scroll to the tracks query and click the lemma for more information.
  5. 5.
    Notice the TYPE DETAILS, listing track properties like id, slug and title.
Mandatory properties
Properties trailed with a ! are non-nullable and mandatory in queries and mutations.
You can also check the API reference. This reference contains the same information as the GraphQL Playground in a different format.

Create and run queries

List all your tracks and their play count

Now you are ready to create and run queries. First, let's create and run a query that lists all your tracks by id, slug , title and play_count:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "query {
    4
    tracks(organizationSlug: \"TEAM\") {
    5
    id
    6
    slug
    7
    title
    8
    play_count
    9
    }
    10
    }"
    11
    }
    12
    EOF
    13
    ​
    14
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
    ⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """query {
7
tracks(organizationSlug: \"TEAM\") {
8
id
9
slug
10
title
11
play_count
12
}
13
}"""
14
​
15
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
16
print(response.json())
Copied!
⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
You will get a response like this:
JSON
1
{
2
"data": {
3
"tracks": [
4
{
5
"id": "amy5xhuaxukc",
6
"slug": "aws-cloud-account",
7
"title": "AWS Cloud Account",
8
"play_count": 52
9
},
10
{
11
"id": "jrwu2g1tymzn",
12
"slug": "azure-subscription",
13
"title": "Azure subscription",
14
"play_count": 29
15
},
16
{
17
"id": "4ttuxz0fgyf1",
18
"slug": "gcp-project",
19
"title": "GCP project",
20
"play_count": 94
21
},
22
{
23
"id": "vnt0zc9owl5c",
24
"slug": "kubernetes",
25
"title": "Kubernetes",
26
"play_count": 201
27
}
28
]
29
}
30
}
Copied!

List challenges in a track and how many times they were started and completed

Next, let's list the challenges in the Kubernetes track with track ID vnt0zc9owl5c and see how many times the challenges were started and finished:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "query {
    4
    challenges(trackID: \"vnt0zc9owl5c\") {
    5
    id
    6
    title
    7
    started
    8
    completed
    9
    }
    10
    }"
    11
    }
    12
    EOF
    13
    ​
    14
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """query {
7
challenges(trackID: "vnt0zc9owl5c") {
8
id
9
title
10
started
11
completed
12
}
13
}"""
14
​
15
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
16
print(response.json())
Copied!
And the response will be something like this:
JSON
1
{
2
"data": {
3
"challenges": [
4
{
5
"id": "gbgigh8crymk",
6
"title": "Deploy NGINX",
7
"started": 173,
8
"completed": 173
9
},
10
{
11
"id": "1xajyu6cyyyz",
12
"title": "Expose the NGINX service",
13
"started": 173,
14
"completed": 124
15
},
16
{
17
"id": "8dxweetsxxox",
18
"title": "Viewing NGINX",
19
"started": 124,
20
"completed": 108
21
},
22
{
23
"id": "h6ejfdyevgqx",
24
"title": "Kubernetes Dashboard"
25
"started": 108,
26
"completed": 108
27
}
28
]
29
}
30
}
Copied!

Create and run mutations

Add a track invite

First, head over to GraphQL Playground, search for the createTrackInvite mutation, and inspect the details.
Next, create the mutation to add a track invite:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "mutation {
    4
    createTrackInvite(invite: {title: \"New invites from ACorp\", trackIDs:[\"TRACK-ID\"], allowAnonymous: true}) {
    5
    id
    6
    title
    7
    }
    8
    }"
    9
    }
    10
    EOF
    11
    ​
    12
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
    ⇨ Replace TRACK-ID with your track ID and YOUR_API_KEY with the copied key. You can also enter your title.
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """mutation {
7
createTrackInvite(invite: {title: "New invites from ACorp", trackIDs:["TRACK-ID"], allowAnonymous: true}) {
8
id
9
title
10
}
11
}"""
12
​
13
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
14
print(response.json())
Copied!
⇨ Replace TRACK-ID with your track ID and YOUR_API_KEY with the copied key. You can also enter your title.
The response will be something like this:
JSON
1
{
2
"data": {
3
"createTrackInvite": {
4
"id": "nm3o1kx5eyna",
5
"title": "New invites from ACorp"
6
}
7
}
8
}
Copied!
And if you move over to your team page in Web UI and click the Invites tab, you will see the newly created track invite.

Add a new team member

You can also add new team members with Instruqt GraphQL API. Again, first head over to GraphQL Playground and now search for the inviteOrganizationUser mutation to inspect the details.
Next, add a new team member with the member role:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "mutation {
    4
    inviteOrganizationUser(organizationID: \"TEAM\", email: \"EMAIL\", role: member) {
    5
    id
    6
    name
    7
    }
    8
    }"
    9
    }
    10
    EOF
    11
    ​
    12
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
    ⇨ Replace TEAM with your team, EMAIL with the new member's email, and YOUR_API_KEY with the copied key.
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """mutation {
7
inviteOrganizationUser(organizationID: "TEAM", email: "EMAIL", role: member) {
8
id
9
name
10
}
11
}"""
12
​
13
​
14
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
15
print(response.json())
Copied!
⇨ Replace TEAM with your team, EMAIL with the new member's email, and YOUR_API_KEY with the copied key.
The response will be something like this:
JSON
1
{
2
"data": {
3
"inviteOrganizationUser": {
4
"id": "p1qlyur60v9g",
5
"name": "Instruqt"
6
}
7
}
8
}
Copied!

Delete a challenge

Inspect the details for the deleteChallenge mutation in GraphQL Playground and then delete the Kubernetes dashboard challenge:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "mutation {
    4
    deleteChallenge(challengeID: \"h6ejfdyevgqx\") {
    5
    }
    6
    }"
    7
    }
    8
    EOF
    9
    ​
    10
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """mutation {
7
deleteChallenge(challengeID: "h6ejfdyevgqx") {
8
}
9
}"""
10
​
11
​
12
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
13
print(response.json())
Copied!
The response will be like this:
JSON
1
{
2
"data": {
3
"deleteChallenge": true
4
}
5
}
Copied!

Handling errors

Every GraphQL (HTTP Post) request responds with the status code 200 (OK), even if your query or mutation contains errors. So, you need to check the query or mutation response for signaling and handling errors. Let's see what an error response looks like by misspelling the organizationSlug property in a query:
Curl
Python
  1. 1.
    Open a terminal and run the following curl example:
  2. 2.
    1
    cat >query.json <<EOF
    2
    {
    3
    "query": "query {
    4
    tracks(organisationSlug: \"TEAM\") {
    5
    id
    6
    slug
    7
    title
    8
    play_count
    9
    }
    10
    }"
    11
    }
    12
    EOF
    13
    ​
    14
    curl -H "Authorization: Bearer YOUR_API_KEY" -X POST -d @query.json https://play.instruqt.com/graphql
    Copied!
    ⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
1
import requests
2
​
3
api_key = "YOUR_API_KEY"
4
endpoint = f"https://play.instruqt.com/graphql"
5
​
6
query = """query {
7
tracks(organisationSlug: \"TEAM\") {
8
id
9
slug
10
title
11
play_count
12
}
13
}"""
14
​
15
response = requests.post(endpoint, headers={"Authorization": "Bearer " + api_key}, json={"query": query})
16
print(response.json())
Copied!
⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
Now you will get an error response like this:
JSON
1
{
2
"errors": [
3
{
4
"message": "Unknown argument \"organisationSug\" on field \"tracks\" of type \"Query\".",
5
"locations": [
6
{
7
"line": 2,
8
"column": 10
9
}
10
]
11
}
12
]
13
}
Copied!
And you can process this error in your script or program.

Example: Updating track invites

Now let's look at a more extensive example in Python that updates all track invites with values set by you. The Python script queries all track invites and runs a mutation query for each track invite:
Python
1
import requests
2
import json
3
​
4
api_key = "YOUR_API_KEY"
5
endpoint = f"https://play.instruqt.com/graphql"
6
team = 'YOUR_TEAM'
7
​
8
# track invite values to be set by you
9
inviteLimit = 500
10
inviteTTL = '2022-07-26T15:00:00Z'
11
playLimit = 2000
12
playTTL = 120
13
allowAnonymous = "false"
14
allowedEmailAddresses = json.dumps(['[email protected]', '[email protected]',
15
'[email protected]', '[email protected]']) # json.dumps replaces single quotes with double quotes as requered by the GraphQL API
16
​
17
# query all track invites
18
query = f"""query {{
19
trackInvites(organizationSlug: \"{team}\") {{
20
id
21
title
22
tracks {{id}}
23
}}
24
}}"""
25
response = requests.post(endpoint, headers={
26
"Authorization": "Bearer " + api_key}, json={"query": query})
27
​
28
# update each individual track invite
29
invites = response.json()['data']['trackInvites']
30
for invite in invites:
31
# parse track invite values to pass them back into the mutation
32
invite_id = invite['id']
33
invite_title = invite['title']
34
invite_tracks = invite['tracks']
35
# create a list with track IDs
36
trackIDs = list()
37
for track in invite_tracks:
38
trackIDs.append(track['id'])
39
trackIDs = json.dumps(trackIDs)
40
​
41
# execute the mutation
42
query = f"""mutation {{updateTrackInvite(invite: {{id: "{invite_id}"
43
title: "{invite_title}",
44
trackIDs: {trackIDs},
45
inviteLimit: {inviteLimit},
46
inviteTTL: "{inviteTTL}",
47
playLimit: {playLimit},
48
playTTL: {playTTL},
49
allowAnonymous: {allowAnonymous},
50
allowedEmailAddresses: {allowedEmailAddresses}}}) {{
51
id
52
title
53
}}
54
}}"""
55
response = requests.post(endpoint, headers={
56
"Authorization": "Bearer " + api_key}, json={"query": query})
57
print('updated:', response.json())
Copied!
⇨ Replace YOUR_API_KEY and YOUR_TEAM with your values. ⇨ Set the variables in lines 9 to 14 with your values.
↳ Lines 18 to 26 queries all track invites. ↳ Line 29 parses all track invites into a variable. ↳ Lines 30 to 57 run over all track invites and updates each track invite with the values you set.
​