Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

How to setup and use the Google Calendar API

For a project I’m working on I needed to interact with a Google Calendar. If you’ve ever worked with some API’s by Google then you know their documentation can be very confusing. It’s not that they don’t have documentation, but code examples of common use cases are simply not present. You must wade through a lot of pages to learn basic things such as how to make an authorized request let alone how to fetch calendar events. In this post I’d like to explain in a human readable way how setup and use the Google Calendar API.

Screen Shot 2016-05-09 at 19.44.40

Getting credentials

The first thing you’ll need to do is to get some credentials to use Google API’s. I’m assuming that you’ve already created a Google account and are signed in. Head over to Google API’s site and click “Select a project” in the header. Choose “Create a new project” in the menu that opens. You can give that project any name you’d like.

Screen Shot 2016-05-09 at 18.55.44

Next up we must specify which API’s the project may consume. In the list of available API’s click “Google Calendar API”. On the next screen click “Enable”.

Screen Shot 2016-05-09 at 18.56.07

Now that you’ve created a project that has access to the Calendar API it’s time to download a file with these credentials. Click “Credentials” in the sidebar. You’ll want to create a “Service account key”.Screen Shot 2016-05-09 at 18.56.48

On the next screen you can give the service account a name. You can name it anything you’d like. In the service account id you’ll see an email address. We’ll use this email address later on in this guide. Select “JSON” as the key type and click “Create” to download the JSON file.

Screen Shot 2016-05-09 at 18.57.18

Setting up the Google Calendar

Now that everything is set up on the API site, we’ll need to configure some things on the Google Calendar site. Head over Google Calendar and view the settings of the calendar you want to work with via PHP.  On the “Share this Calendar” tab add the service account id that was displayed when creating credentials on the API site. Screen Shot 2016-05-09 at 19.10.52

Open up the “Calendar Details” tab to see the id of the calendar. We’ll need this id later on.

Screen Shot 2016-05-09 at 19.19.10

Accessing the calendar in PHP

Now that everything is set up at Google, let’s finally write some PHP.  In your project make sure you require google/apiclient in your composer.json. This piece of code can be used to fetch all events on your calendar:

$client = new Google_Client();

// $credentialsJson should contain the path to the json file downloaded from the API site
$credentials = $client->loadServiceAccountJson(


$service = new Google_Service_Calendar($client);

// $calendarId should contain the calendar id found on the settings page of the calendar
$events = $service->events->listEvents($calendarId);

In the example above all events will get fetched. Of course the API can do a whole lot more. Take a look at the API docs to learn what’s possible.

In closing

Setting up and using the calendar API, and the other Google API’s is not that difficult. The key thing that’s missing is proper documentation. I hope that in the future Google will add some easy to follow tutorials and practical examples to their docs. It took me about an hour to learn and set everything mentioned in this post. With good documentation the required time would be around 5 minutes (and there would be much less frustration).

If you’re using Laravel you’ll be happy to know that I’m currently developing a package that makes working with a Google Calendar a breeze. My goal is to make this code work:

use \Spatie\GoogleCalendar\Event;

//creating events
$event = new Event();
$event->title = 'My new event'
$event->start = Carbon::now();
$event->end = Carbon::now()->addHour();

//fetching all events
$allEvents = Event::all();

//updating an event
$event = $allEvents->first();
$event->name = 'updated name';

//deleting an event

I’m not planning on supporting all the features of the native API, but if you need only the basic functionality, this will be the package for you.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • DigitalCoder

    Great !
    Thx for Laravel Package !

    @+ DigitalCoder

  • Eldair

    Couldn’t agree more with you, finding the right thing in their documentation is like finding needle in a haystack.

  • Pingback: A package to manage events on a Google Calendar - murze.be()

  • Pingback: Easily interact with Google Calendar from PHP with spatie/laravel-google-calendar | Bram.us()

  • Pingback: Laravel Analytics v2 has been released - murze.be()

  • Thank you for a great tutorial, I really miss screenshots in Google’s documentation!

    However, I’m not experienced enough to understand how to do the last part and I am confused. Is it possible for you to give the code of a full php-file? With comments to where we should change the variables to our specifics?

    I tried to put som echo-statements in the code, just to test where it stops. And it seems that already at

    $client = new Google_Client();

    the code stops to run.

    I also don’t understand “In your project make sure you require google/apiclient in your composer.json.” Should I edit the json-file I downloaded or is this another json-file?

    Thank your for any hint and help you might give me!

  • Adrian Raudaschl

    Thanks so much for the amazing tutorial.

    I’m currently having problems generating the correct json file. Currently when I generate the json file it creates a “service_account” type json file.

    When i’m using the spatie/laravel-google-calendar package however it requires a “client secret” json. Somehow I managed to get this to work once but have been unable to replicate it.

    I feel like I’m missing something obvious. Please help.

  • rajesh

    can you send me php complete source code

  • Sheetal Php

    Hello Freek Van der Herten

    I did all step by step as u described in document , but I got an error while running php file.

    Error->”( ! ) Fatal error: Class ‘Google_Client’ not found in C:wampwwwg..”

    Will u please explain this–>”In your project make sure you require google/apiclient in your composer.json.”
    what u mean by this.I am not getting this.

    Please Help.

  • QuantumTiger

    If LoadServiceAccountJson is not available in the API replace
    $credentials = $client->loadServiceAccountJson($credentialJson,


    as per https://github.com/google/google-api-php-client/blob/master/UPGRADING.md