Back

Step by Step Guide to Building a Linear API Integration in JS

Aug 13, 20245 minute read

Introduction

Hey there, fellow dev! Ready to supercharge your project management workflow? Let's dive into integrating Linear's powerful API using their nifty @linear/sdk package. Trust me, it's easier than you might think!

Prerequisites

Before we jump in, make sure you've got:

  • Node.js and npm (you're a pro, so I'm sure you've got this covered)
  • A Linear account and API key (if you don't have one, hop over to Linear and grab it real quick)

Setting up the project

Let's get the boring stuff out of the way:

mkdir linear-integration && cd linear-integration npm init -y npm install @linear/sdk

Authenticating with Linear

Now for the fun part. Let's get you authenticated:

import { LinearClient } from "@linear/sdk"; const linearClient = new LinearClient({ apiKey: 'your_api_key_here' });

Easy peasy, right?

Basic API operations

Let's flex those API muscles with some CRUD operations:

Fetching issues

const issues = await linearClient.issues(); console.log(issues);

Creating an issue

const newIssue = await linearClient.createIssue({ title: "Fix that pesky bug", description: "It's driving everyone nuts!", teamId: "your_team_id" });

Updating an issue

await linearClient.updateIssue(issueId, { title: "Actually, let's postpone fixing that bug", state: "Backlog" });

Deleting an issue

await linearClient.deleteIssue(issueId);

Advanced usage

Ready to level up? Let's go!

Querying with GraphQL

const { data } = await linearClient.client.rawQuery(` query { issues(first: 10) { nodes { id title state { name } } } } `);

Pagination

let allIssues = []; let hasNextPage = true; let endCursor = null; while (hasNextPage) { const { issues } = await linearClient.issues({ first: 100, after: endCursor, }); allIssues = [...allIssues, ...issues.nodes]; hasNextPage = issues.pageInfo.hasNextPage; endCursor = issues.pageInfo.endCursor; }

Error handling

try { // Your Linear API calls here } catch (error) { if (error.type === 'AuthenticationError') { console.error('Check your API key!'); } else { console.error('Oops, something went wrong:', error.message); } }

Webhooks integration

Want real-time updates? Webhooks to the rescue!

// Set up your webhook endpoint app.post('/linear-webhook', (req, res) => { const event = req.body; if (event.type === 'Issue') { console.log('Issue updated:', event.data); } res.sendStatus(200); });

Best practices

  • Respect rate limits: Linear's pretty generous, but don't go crazy with requests.
  • Cache when you can: Your future self will thank you.

Conclusion

And there you have it! You're now armed and dangerous with Linear API integration skills. Go forth and build amazing things! Remember, the Linear API docs are your friend if you need more details.

Happy coding, you magnificent developer, you! 🚀