Open jln13x opened 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:
Ensure the user is a system-wide admin or the event type belongs to the user:
eventTypeId
must belong to the user making the request.userId
in the request body is valid.Determine the team ID from the event type:
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;
}
}
Prepare the request body:
subscriberUrl
, eventTriggers
, active
, and optionally payloadTemplate
, eventTypeId
, and secret
.Make the POST request to /webhooks
:
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.
Found a bug? Please fill out the sections below. 👍
Issue Summary
/webhooks
fails if theeventTypeId
is from a teamapps/api/v1/pages/api/webhooks/_post.ts
L84 because its being checked if the eventType belongs to the userIdSteps to Reproduce
/v1/webhooks
endpointExample Payload: