calcom / cal.com

Scheduling infrastructure for absolutely everyone.
https://cal.com
Other
31.83k stars 7.78k forks source link

Fetching Availability Slots on v2 API results in TRPCError #16759

Open govindrai opened 3 weeks ago

govindrai commented 3 weeks ago

Found a bug? Please fill out the sections below. 👍

Issue Summary

When trying to fetch availability slots using the v2 API, I get the following error:

Error: {
  "status": 404,
  "statusText": "Not Found",
  "errorResponse": {
    "status": "error",
    "timestamp": "2024-09-22T16:30:39.851Z",
    "path": "/v2/slots/available?startTime=2024-09-24T07%3A00%3A00.000Z&endTime=2024-09-25T06%3A59%3A59.999Z",
    "error": {
      "code": "TRPCError",
      "message": "NOT_FOUND"
    }
  }
}

Steps to Reproduce

Run this on Node.js 21

require("dotenv").config()
const util = require("util")
util.inspect.defaultOptions.depth = 25

async function fetchAvailability(startTime, endTime) {
    const apiKey = process.env.CAL_API_KEY; // Replace with your Cal.com API key

    const url = new URL(`https://api.cal.com/v2/slots/available`);
    url.searchParams.append("startTime", startTime)
    url.searchParams.append("endTime", endTime)
    try {
        const response = await fetch(url, {
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Content-Type': 'application/json',
            },
            method: "GET",
        });

        if (!response.ok) {
            const error = {
                status: response.status,
                statusText: response.statusText,
                errorResponse: await response.json()
            }
            throw new Error(JSON.stringify(error, null, 2));
        }

        const data = await response.json();
        return data;
    } catch (error) {
        throw error;
    }
}

// Helper function to get the end of a day given a date in ISO format
function getEndOfDayUsingStartTime(date) {
    return new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        23, 59, 59, 999
    ); // Set the time to the end of the day
}

const date = new Date("09/24/2024")
fetchAvailability(date.toISOString(), getEndOfDayUsingStartTime(date).toISOString())
    .then(console.log)

I expect to either get a 400 (Bad Request) or availability slots.

josecardozo commented 2 weeks ago

I have the same issue here:

curl --location 'https://api.cal.com/v2/slots/available?startTime=2024-09-01T03%3A00%3A00.000Z&endTime=2024-10-01T02%3A59%3A59.999Z' \
--header 'Authorization: cal_<my_token>'

Returns:

{
    "status": "error",
    "timestamp": "2024-09-24T00:20:25.518Z",
    "path": "/v2/slots/available?startTime=2024-09-01T03%3A00%3A00.000Z&endTime=2024-10-01T02%3A59%3A59.999Z&eventTypeSlug=%2230min%22",
    "error": {
        "code": "TRPCError",
        "message": "NOT_FOUND"
    }
}
sarveshcoditron commented 1 week ago

You have to add "eventTypeId" or "username/eventslug" as parameter as well