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:
query { tracks(organizationSlug: \"instruqt\") { id slug title }}
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:
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:
Click Settings → API keys.
Click Generate API Key.
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:
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:
Add the following HTTP header:
{"Authorization":"Bearer YOUR_API_KEY"}
⇨ Replace YOUR_API_KEY with the copied key.
In the Endpoint field, enter https://play.instruqt.com/graphql.
Click the Docs tab.
↳ A list with QUERIES and MUTATIONS opens.
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.
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:
Open a terminal and run the following curl example:
cat>query.json<<EOF{ "query": "query { tracks(organizationSlug: \"TEAM\") { id slug title play_count } }"}EOFcurl-H"Authorization: Bearer YOUR_API_KEY"-XPOST-d@query.jsonhttps://play.instruqt.com/graphql
⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
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:
Open a terminal and run the following curl example:
cat>query.json<<EOF{ "query": "query { challenges(trackID: \"vnt0zc9owl5c\") { id title started completed } }"}EOFcurl-H"Authorization: Bearer YOUR_API_KEY"-XPOST-d@query.jsonhttps://play.instruqt.com/graphql
import requestsapi_key ="YOUR_API_KEY"endpoint =f"https://play.instruqt.com/graphql"query ="""query { challenges(trackID: "vnt0zc9owl5c") { id title started completed }}"""response = requests.post(endpoint, headers={"Authorization": "Bearer "+ api_key}, json={"query": query})print(response.json())
First, head over to GraphQL Playground, search for the createTrackInvite mutation, and inspect the details.
Next, create the mutation to add a track invite:
Open a terminal and run the following curl example:
cat>query.json<<EOF{ "query": "mutation { createTrackInvite(invite: {title: \"New invites from ACorp\", trackIDs:[\"TRACK-ID\"], allowAnonymous: true}) { id title } }"}EOFcurl-H"Authorization: Bearer YOUR_API_KEY"-XPOST-d@query.jsonhttps://play.instruqt.com/graphql
⇨ Replace TRACK-ID with your track ID and YOUR_API_KEY with the copied key. You can also enter your title.
import requestsapi_key ="YOUR_API_KEY"endpoint =f"https://play.instruqt.com/graphql"query ="""mutation { createTrackInvite(invite: {title: "New invites from ACorp", trackIDs:["TRACK-ID"], allowAnonymous: true}) { id title }}"""response = requests.post(endpoint, headers={"Authorization": "Bearer "+ api_key}, json={"query": query})print(response.json())
⇨ 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:
{"data": {"createTrackInvite": {"id":"nm3o1kx5eyna","title":"New invites from ACorp" } }}
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:
Open a terminal and run the following curl example:
cat>query.json<<EOF{ "query": "mutation { inviteOrganizationUser(organizationID: \"TEAM\", email: \"EMAIL\", role: member) { id name } }"}EOFcurl-H"Authorization: Bearer YOUR_API_KEY"-XPOST-d@query.jsonhttps://play.instruqt.com/graphql
⇨ Replace TEAM with your team, EMAIL with the new member's email, and YOUR_API_KEY with the copied key.
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:
Open a terminal and run the following curl example:
cat>query.json<<EOF{ "query": "query { tracks(organisationSlug: \"TEAM\") { id slug title play_count } }"}EOFcurl-H"Authorization: Bearer YOUR_API_KEY"-XPOST-d@query.jsonhttps://play.instruqt.com/graphql
⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
⇨ Replace TEAM with your team and YOUR_API_KEY with the copied key.
Now you will get an error response like this:
{"errors": [ {"message":"Unknown argument \"organisationSug\" on field \"tracks\" of type \"Query\".","locations": [ {"line":2,"column":10 } ] } ]}
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:
import requestsimport jsonapi_key ="YOUR_API_KEY"endpoint =f"https://play.instruqt.com/graphql"team ='YOUR_TEAM'# track invite values to be set by youinviteLimit =500inviteTTL ='2022-07-26T15:00:00Z'playLimit =2000playTTL =120allowAnonymous ="false"allowedEmailAddresses = json.dumps(['june@mycorp.com', 'adam@mycorp.com', 'angela@my-corp.com', 'pete@mycorp.com']) # json.dumps replaces single quotes with double quotes as requered by the GraphQL API
# query all track invitesquery = f"""query {{ trackInvites(organizationSlug: \"{team}\") {{ id title tracks {{id}}}}}}"""response = requests.post(endpoint, headers={"Authorization": "Bearer "+ api_key}, json={"query": query})# update each individual track inviteinvites = response.json()['data']['trackInvites']for invite in invites:# parse track invite values to pass them back into the mutation invite_id = invite['id'] invite_title = invite['title'] invite_tracks = invite['tracks']# create a list with track IDs trackIDs = list()for track in invite_tracks: trackIDs.append(track['id']) trackIDs = json.dumps(trackIDs)# execute the mutation query = f"""mutation {{updateTrackInvite(invite: {{id: "{invite_id}" title: "{invite_title}", trackIDs: {trackIDs}, inviteLimit: {inviteLimit}, inviteTTL: "{inviteTTL}", playLimit: {playLimit}, playTTL: {playTTL}, allowAnonymous: {allowAnonymous}, allowedEmailAddresses: {allowedEmailAddresses}}}) {{ id title}}}}""" response = requests.post(endpoint, headers={"Authorization": "Bearer "+ api_key}, json={"query": query})print('updated:', response.json())
⇨ 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.