Edits history of script submission #12119 for ' Create a webhook (figma)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Figma = {
      token: string;
    };
    /**
     * Create a webhook
     * Create a new webhook which will call the specified endpoint when the event triggers. By default, this webhook will automatically send a PING event to the endpoint when it is created. If this behavior is not desired, you can create the webhook and set the status to PAUSED and reactivate it later.
     */
    export async function main(
      auth: Figma,
      body: {
        event_type:
          | "PING"
          | "FILE_UPDATE"
          | "FILE_VERSION_UPDATE"
          | "FILE_DELETE"
          | "LIBRARY_PUBLISH"
          | "FILE_COMMENT";
        team_id: string;
        endpoint: string;
        passcode: string;
        status?: "ACTIVE" | "PAUSED";
        description?: string;
      },
    ) {
      const url = new URL(`https://api.figma.com/v2/webhooks`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago