Back

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

Aug 14, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your time tracking game? Let's dive into building a Clockify API integration using PHP. We'll be leveraging the awesome jdecool/clockify-api package to make our lives easier. Buckle up!

Prerequisites

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

  • A PHP environment (you're a pro, so I'm sure you've got this covered)
  • Composer installed (because who doesn't love dependency management?)
  • A Clockify account with an API key (if you don't have one, go grab it from your Clockify settings)

Installation

First things first, let's get that package installed:

composer require jdecool/clockify-api

Easy peasy, right?

Setting up the Clockify Client

Now, let's initialize our Clockify client:

use JDecool\Clockify\Client; $apiKey = 'your-api-key-here'; $client = new Client($apiKey);

Basic Operations

Fetching Workspaces

Let's start with something simple:

$workspaces = $client->workspaces()->workspaces(); foreach ($workspaces as $workspace) { echo $workspace->name . "\n"; }

Retrieving Projects

Now, let's grab some projects:

$workspaceId = 'your-workspace-id'; $projects = $client->projects()->projects($workspaceId); foreach ($projects as $project) { echo $project->name . "\n"; }

Getting Time Entries

Time to fetch those time entries:

$userId = 'your-user-id'; $timeEntries = $client->timeEntries()->findAllTimeEntries($workspaceId, $userId); foreach ($timeEntries as $entry) { echo $entry->description . ": " . $entry->timeInterval->duration . "\n"; }

Advanced Usage

Creating Time Entries

Let's add a new time entry:

$entry = $client->timeEntries()->createTimeEntry($workspaceId, [ 'start' => '2023-06-01T09:00:00Z', 'end' => '2023-06-01T17:00:00Z', 'description' => 'Coding like a boss', 'projectId' => 'your-project-id' ]);

Updating Time Entries

Need to make a change? No problem:

$client->timeEntries()->updateTimeEntry($workspaceId, $entry->id, [ 'description' => 'Coding like a SUPER boss' ]);

Deleting Time Entries

Oops, made a mistake? Let's clean it up:

$client->timeEntries()->deleteTimeEntry($workspaceId, $entry->id);

Error Handling

Always be prepared for the unexpected:

try { // Your Clockify API calls here } catch (\JDecool\Clockify\Exception\ClockifyException $e) { echo "Oops! Something went wrong: " . $e->getMessage(); }

Best Practices

  • Keep an eye on those rate limits. Clockify's not too strict, but it's always good to be mindful.
  • Consider caching frequently accessed data to reduce API calls and speed up your app.

Example Use Case: Simple Time Tracking Dashboard

Let's put it all together:

$workspaceId = 'your-workspace-id'; $userId = 'your-user-id'; $projects = $client->projects()->projects($workspaceId); $timeEntries = $client->timeEntries()->findAllTimeEntries($workspaceId, $userId); echo "<h1>My Time Tracking Dashboard</h1>"; echo "<h2>Projects:</h2>"; echo "<ul>"; foreach ($projects as $project) { echo "<li>{$project->name}</li>"; } echo "</ul>"; echo "<h2>Recent Time Entries:</h2>"; echo "<ul>"; foreach ($timeEntries as $entry) { echo "<li>{$entry->description}: {$entry->timeInterval->duration}</li>"; } echo "</ul>";

Conclusion

And there you have it! You're now equipped to build some seriously cool time tracking integrations with Clockify. Remember, this is just scratching the surface - there's so much more you can do with the API. So go forth and code, you time-tracking wizard!

Need more info? Check out the Clockify API docs and the jdecool/clockify-api GitHub repo. Happy coding!