calcom / cal.com

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

Creating a Webhook for Event Type of Team #15449

Open jln13x opened 2 months ago

jln13x commented 2 months ago

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

Issue Summary

Steps to Reproduce

Example Payload:

{
  "subscriberUrl": "https://example.com",
  "eventTriggers": ["BOOKING_CREATED"],
  "active": true,
  "eventTypeId": ...
}
dosubot[bot] commented 2 months ago

Hey there, @jln13x! I'm here to help you with any bugs, questions, or contribution guidance you might need. Let's get this issue sorted out together.

To create a webhook for a specific event type of your team using the API, you need to ensure that the eventTypeId is correctly associated with the team and that the user making the request has the necessary permissions. Here is a step-by-step guide:

  1. Ensure the user is a system-wide admin or the event type belongs to the user:

    • If the user is not a system-wide admin, the eventTypeId must belong to the user making the request.
    • If the user is a system-wide admin, additional checks are performed to ensure the userId in the request body is valid.
  2. Determine the team ID from the event type:

    • Use the getTeamIdFromEventType function to retrieve the team ID associated with the event type. This function checks if the event type has a direct team ID or if it is a managed event, in which case it retrieves the team ID from the parent event type.
    import prisma from "@calcom/prisma";
    
    export async function getTeamIdFromEventType({
     eventType,
    }: {
     eventType: { team: { id: number | null } | null; parentId: number | null };
    }) {
     if (!eventType) {
       return null;
     }
    
     if (eventType?.team?.id) {
       return eventType.team.id;
     }
    
     // If it's a managed event we need to find the teamId for it from the parent
     if (eventType?.parentId) {
       const managedEvent = await prisma.eventType.findFirst({
         where: {
           id: eventType.parentId,
         },
         select: {
           teamId: true,
         },
       });
    
       return managedEvent?.teamId;
     }
    }
  3. Prepare the request body:

    • Include the subscriberUrl, eventTriggers, active, and optionally payloadTemplate, eventTypeId, and secret.
  4. Make the POST request to /webhooks:

    • Ensure the request includes the necessary API key and the correct JSON body.

Here is an example of how to structure the POST request:

{
  "subscriberUrl": "https://example.com/webhook",
  "eventTriggers": ["BOOKING_CREATED"],
  "active": true,
  "eventTypeId": 123,  // Replace with the actual event type ID
  "payloadTemplate": "{ \"example\": \"data\" }",
  "secret": "your-secret-key"
}

Example cURL command:

curl -X POST https://api.cal.com/webhooks \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "subscriberUrl": "https://example.com/webhook",
    "eventTriggers": ["BOOKING_CREATED"],
    "active": true,
    "eventTypeId": 123,
    "payloadTemplate": "{ \"example\": \"data\" }",
    "secret": "your-secret-key"
  }'

Ensure that the eventTypeId is valid and that the user making the request has the necessary permissions to create a webhook for that event type. If the request fails, check the error message for more details on what might be wrong (e.g., invalid eventTypeId, insufficient permissions) [1][2].

Regarding API keys for teams, you are correct that there is no direct way to get an API key specifically for a team. API keys are generated and managed through the createHandler function, which ensures that only users with the role of OWNER or ADMIN can create API keys for a team [3].

To continue talking to Dosu, mention @dosu.