Testing the Linear GraphQL API with an API key

Linear's API is GraphQL only: one endpoint, POST everything, and the shape of the answer is the shape of your query. That makes it pleasant to explore and slightly different to test. The status code tells you less than you'd expect, and the errors array tells you more.

Step 1: Key and environment

In Linear's settings, under API, create a personal API key. Use a test workspace or a test team.

VariableValue
linearUrlhttps://api.linear.app/graphql
linearKeylin_api_…, marked as secret

Use API key auth with header name Authorization and value {{linearKey}}. Create the requests with the GraphQL body type, which gives you schema introspection and autocomplete.

Step 2: Who am I, and which teams?

GraphQL
query Me {
  viewer { id name email }
  teams { nodes { id key name } }
}
JavaScript
const body = pm.response.json();
pm.test('no GraphQL errors', () => pm.expect(body.errors).to.not.exist);
pm.test('viewer returned', () => pm.expect(body.data.viewer.id).to.be.ok);
pm.environment.set('teamId', body.data.teams.nodes[0].id);

Put the first two tests in the collection's post-response script. Every request in the collection then checks for errors, and the expected failures go in their own folder.

Step 3: Create an issue, with variables

GraphQL
mutation CreateIssue($teamId: String!, $title: String!) {
  issueCreate(input: {teamId: $teamId, title: $title, description: "Created by API tests"}) {
    success
    issue { id identifier url }
  }
}

Variables:

JSON
{"teamId": "{{teamId}}", "title": "API test {{$timestamp}}"}
JavaScript
const result = pm.response.json().data.issueCreate;
pm.test('issue created', () => {
  pm.expect(result.success).to.equal(true);
  pm.expect(result.issue.identifier).to.match(/^[A-Z]+-\d+$/);
});
pm.environment.set('issueId', result.issue.id);

Building queries by pasting values into the string breaks on the first title with a quote in it. Variables don't.

Step 4: Filter and paginate

GraphQL
query OpenIssues($teamId: ID!, $after: String) {
  issues(first: 50, after: $after, filter: {team: {id: {eq: $teamId}}, state: {type: {neq: "completed"}}}) {
    nodes { identifier title }
    pageInfo { hasNextPage endCursor }
  }
}

Linear uses Relay-style connections: first/after, and pageInfo.endCursor for the next page. The same check works for every list.

Step 5: Errors

GraphQL
mutation { issueCreate(input: {teamId: "not-a-team", title: "x"}) { success } }
JavaScript
pm.test('invalid team reported', () => {
  const body = pm.response.json();
  pm.expect(body.errors).to.be.an('array').that.is.not.empty;
  pm.expect(body.errors[0]).to.have.property('message');
});

Linear answers some GraphQL errors with 400 and some with 200, depending on the kind. A test that only checks the status code will miss half of them, so always look at errors.

Linear also limits by query complexity, not just request count: a query asking for 250 issues with nested comments costs far more than a small one. Keep test queries small.

Clean up with the issueArchive or issueDelete mutation in a cleanup step.