Back

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

Aug 13, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your project management workflow with Linear? Let's dive into building a slick API integration using PHP and the awesome blomstra/linear package. This guide will get you up and running in no time, so let's get cracking!

Prerequisites

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

  • PHP 7.4 or higher (come on, you're not still using 5.6, right?)
  • Composer installed (because who wants to manage dependencies manually?)
  • A Linear API key (grab one from your Linear account settings)

Installation

First things first, let's get that blomstra/linear package installed. Fire up your terminal and run:

composer require blomstra/linear

Easy peasy, right?

Configuration

Now, let's set up our Linear client. It's as simple as:

use Blomstra\Linear\Client; $linear = new Client('your-api-key-here');

Boom! You're authenticated and ready to roll.

Basic Usage

Fetching Issues

Want to grab some issues? Here's how:

$issues = $linear->issues()->get(); foreach ($issues as $issue) { echo $issue->title . "\n"; }

Creating Issues

Got a new task? Let's add it to Linear:

$newIssue = $linear->issues()->create([ 'title' => 'Fix that pesky bug', 'description' => 'It\'s driving everyone crazy!', 'teamId' => 'your-team-id' ]);

Updating Issues

Need to make changes? No sweat:

$linear->issues()->update('issue-id', [ 'status' => 'In Progress' ]);

Advanced Features

Working with Teams

Get your team info like a boss:

$teams = $linear->teams()->get();

Managing Projects

Create a new project to keep things organized:

$project = $linear->projects()->create([ 'name' => 'World Domination', 'teamId' => 'your-team-id' ]);

Handling Comments

Add some context with comments:

$linear->comments()->create([ 'body' => 'Great progress on this!', 'issueId' => 'issue-id' ]);

Error Handling

Don't let API hiccups throw you off your game. Wrap your calls in try-catch blocks:

try { $result = $linear->issues()->get(); } catch (\Exception $e) { echo "Oops! " . $e->getMessage(); }

And keep an eye on those rate limits. The package handles them gracefully, but it's good to be aware.

Best Practices

Caching Responses

Save some API calls and speed things up by caching responses:

$cache->remember('linear_issues', 3600, function () use ($linear) { return $linear->issues()->get(); });

Pagination Handling

Don't let large datasets slow you down. Use pagination:

$allIssues = []; $page = 1; do { $issues = $linear->issues()->get(['page' => $page]); $allIssues = array_merge($allIssues, $issues); $page++; } while (count($issues) > 0);

Example Integration

Let's put it all together with a real-world scenario. Say you want to sync Linear issues with your custom dashboard:

function syncLinearIssues($linear, $dashboard) { $issues = $linear->issues()->get(['updatedAt' => ['gt' => 'last_sync_time']]); foreach ($issues as $issue) { $dashboard->updateOrCreateIssue([ 'external_id' => $issue->id, 'title' => $issue->title, 'status' => $issue->status, 'assignee' => $issue->assignee->name ?? null ]); } }

Conclusion

And there you have it! You're now equipped to build a robust Linear API integration in PHP. Remember, the blomstra/linear package is your friend, making Linear API interactions a breeze.

Keep exploring the package documentation for more features, and don't be afraid to get creative with your integrations. Happy coding, and may your projects be ever organized and your teams ever productive!