Using the Linear API to Get Issues in PHP

by Endgrate Team 2024-08-05 5 min read

Linear homepage

Introduction to Linear's API for Issue Management

Linear is a modern issue tracking tool designed to help teams manage their workflows efficiently. Known for its sleek design and powerful features, Linear enables teams to track bugs, plan sprints, and manage projects with ease. Its API offers robust capabilities for developers looking to integrate issue management into their applications.

Connecting with Linear's API allows developers to automate and streamline issue tracking processes. For example, a developer might want to retrieve issues from Linear to display them in a custom dashboard or integrate them with other project management tools. This can enhance productivity by providing a unified view of tasks and issues across different platforms.

Setting Up a Linear Test Account for API Integration

Before you can start interacting with Linear's API, you'll need to set up a test or sandbox account. This will allow you to safely experiment with API calls without affecting live data. Linear offers a straightforward process for developers to create and manage OAuth2 applications, which is essential for authenticating API requests.

Creating a Linear OAuth2 Application

  1. Sign Up for a Linear Account: If you don't already have a Linear account, visit the Linear website and sign up for a free account. This will give you access to the necessary tools and settings to create an OAuth2 application.
  2. Create a New OAuth2 Application: Navigate to the settings page in your Linear account. Under the "Integrations" section, select "OAuth2 Applications" and click on "Create New Application."
  3. Configure Redirect URIs: During the application setup, you'll need to specify the redirect URIs. These are the URLs to which users will be redirected after they authorize your application. Ensure these URIs are correctly configured to match your application's domain.

Authorizing Users with Linear OAuth2

Once your OAuth2 application is set up, you'll need to handle user authorization. This involves redirecting users to Linear's authorization URL with the appropriate parameters.


GET https://linear.app/oauth/authorize?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URL&scope=read,write

Replace YOUR_CLIENT_ID and YOUR_REDIRECT_URL with your application's client ID and redirect URI, respectively. The scope parameter defines the level of access your application requires.

Exchanging Authorization Code for Access Token

After users authorize your application, they will be redirected back to your specified redirect URI with an authorization code. You must exchange this code for an access token to authenticate API requests.


POST https://api.linear.app/oauth/token
Content-Type: application/x-www-form-urlencoded

Parameters:
- code: Authorization code from the previous step
- redirect_uri: Same redirect URI used previously
- client_id: Your application's client ID
- client_secret: Your application's client secret
- grant_type: authorization_code

Upon successful exchange, you'll receive an access token, which you can use to make authenticated requests to Linear's API.

For more detailed information on setting up OAuth2 authentication with Linear, refer to the official documentation: Linear OAuth Authentication Documentation.

Linear authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve Issues from Linear Using PHP

To interact with Linear's API and retrieve issues, you'll need to use PHP to make HTTP requests. This section will guide you through the process of setting up your PHP environment, making the API call, and handling the response.

Setting Up Your PHP Environment for Linear API Integration

Before making API calls, ensure you have PHP installed on your system. This tutorial assumes you are using PHP 7.4 or later. Additionally, you'll need the cURL extension enabled, which is commonly used for making HTTP requests in PHP.

To install the necessary dependencies, you can use Composer, a dependency manager for PHP. Run the following command to install the guzzlehttp/guzzle library, which simplifies HTTP requests:

composer require guzzlehttp/guzzle

Executing a GraphQL Query to Fetch Issues from Linear

With your environment set up, you can now write a PHP script to query Linear's API for issues. Create a file named get_linear_issues.php and add the following code:


post('https://api.linear.app/graphql', [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
        'Content-Type' => 'application/json',
    ],
    'json' => [
        'query' => '{
            issues {
                nodes {
                    id
                    title
                    description
                }
            }
        }'
    ]
]);

$data = json_decode($response->getBody(), true);

foreach ($data['data']['issues']['nodes'] as $issue) {
    echo "ID: " . $issue['id'] . "\n";
    echo "Title: " . $issue['title'] . "\n";
    echo "Description: " . $issue['description'] . "\n\n";
}
?>

Replace YOUR_ACCESS_TOKEN with the access token obtained during the OAuth2 authentication process. This script uses Guzzle to send a POST request to Linear's GraphQL endpoint, querying for issue details.

Verifying Successful API Requests and Handling Errors

After running the script, you should see a list of issues printed in your terminal. This confirms that the API call was successful. If you encounter errors, check the response status code and message for more details.

Common error codes include:

  • 400 Bad Request: The request was malformed. Check your query syntax.
  • 401 Unauthorized: The access token is missing or invalid. Ensure your token is correct.
  • 429 Too Many Requests: You've hit the rate limit. Linear's API allows a certain number of requests per minute. Consider implementing rate limiting in your application.

For more information on error handling, refer to the Linear API documentation.

Conclusion and Best Practices for Using Linear API with PHP

Integrating with Linear's API using PHP provides a powerful way to automate and enhance your issue management processes. By following the steps outlined in this guide, you can efficiently retrieve and manage issues within your applications, leveraging Linear's robust API capabilities.

Best Practices for Secure and Efficient API Integration

  • Securely Store Access Tokens: Always store your OAuth2 access tokens securely. Consider using environment variables or secure vaults to keep them safe from unauthorized access.
  • Implement Rate Limiting: Be mindful of Linear's API rate limits to avoid disruptions. Implement logic to handle the 429 Too Many Requests error by queuing requests or using exponential backoff strategies.
  • Standardize Data Handling: Ensure consistent data formats when integrating Linear issues with other systems. This may involve transforming or mapping data fields to match your application's requirements.

Enhancing Integration Capabilities with Endgrate

For developers looking to streamline multiple integrations, consider using Endgrate. With Endgrate, you can save time and resources by outsourcing integrations and focusing on your core product. Build once for each use case and enjoy an intuitive integration experience for your customers.

Explore how Endgrate can simplify your integration processes by visiting Endgrate's website.

Read More

Ready to get started?

Book a demo now

Book Demo