Get Holidays List from Google Calendar API

 import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
import requests
from datetime import datetime
from typing import List

logger = logging.getLogger(__name__)

app = FastAPI()

    raise Exception("The GOOGLE_CALENDAR_API_KEY environment variable is not set.")

def fetch_holidays(country_code: str, year: str):
    url = f"{country_code}"
    params = {
        'timeMin': f'{year}-01-01T00:00:00Z',
        'timeMax': f'{year}-12-31T23:59:59Z',
        'singleEvents': True,

About this template

A holiday fetching app that uses the Google Calendar API to display holidays based on country and year. Google calendar api allows to fetch holidays for current, previous and next year only. Requires GOOGLE_API_KEY to run.

Introduction to the Get Holidays List from Google Calendar API Template

Welcome to the Get Holidays List from Google Calendar API template! This template is designed to help you build an application that fetches holiday information for a specific country and year using the Google Calendar API. It's a great tool for developers who want to integrate holiday data into their applications without having to worry about the complexities of API integration and server setup.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial Setup: Adding Environment Secrets

Before you can use this template, you'll need to set up an environment secret for the Google Calendar API key. Here's how to do it:

  • Visit the Google Developers Console and create a new project or select an existing one.
  • Enable the Google Calendar API for your project.
  • Go to the "Credentials" section and create an API key.
  • Copy the API key you just created.
  • In the Lazy Builder interface, navigate to the Environment Secrets tab.
  • Create a new secret with the key `GOOGLE_CALENDAR_API_KEY` and paste the API key you copied as the value.

Test: Pressing the Test Button

Once you have set up your environment secret, you can test the application by clicking the "Test" button. This will deploy your app and launch the Lazy CLI.

Using the App

After pressing the "Test" button, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also be provided with a link to the API documentation.

To fetch holidays for a specific country and year, you will use the provided server link followed by the endpoint pattern `/holidays/{country_code}/{year}`. Replace `{country_code}` with the desired country code and `{year}` with the year you want to fetch holidays for.

Here's an example of how to make a request:

GET http://your-server-link/holidays/en.usa/2023

You should expect a response similar to this:

    "kind": "calendar#event",
    "etag": "\"etag\"",
    "id": "event_id",
    "status": "confirmed",
    "htmlLink": "",
    "created": "datetime",
    "updated": "datetime",
    "summary": "New Year's Day",
    "description": "New Year's Day is a public holiday in the USA.",

Integrating the App

If you want to integrate this holiday information into another service or frontend, you can use the server link provided by Lazy to make API requests from your application. Ensure that you handle the API responses correctly and display the holiday data as needed in your application.

For example, if you're building a calendar application, you can use the fetched holiday data to mark holidays on the calendar for users based on their country and year selection.

Remember to respect the Google Calendar API usage limits and terms of service when integrating and using the API in your application.

By following these steps, you should now have a functional application that can fetch and display holiday information from the Google Calendar API. Happy building!

Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.


A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .


WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

Get Holidays List from Google Calendar API