Edits history of script submission #12021 for ' Merge and update a user's metadata (clerk)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Clerk = {
      apiKey: string;
    };
    /**
     * Merge and update a user's metadata
     * Update a user's metadata attributes by merging existing values with the provided parameters.
    
    This endpoint behaves differently than the *Update a user* endpoint.
    Metadata values will not be replaced entirely.
    Instead, a deep merge will be performed.
    Deep means that any nested JSON objects will be merged as well.
    
    You can remove metadata keys at any level by setting their value to `null`.
     */
    export async function main(
      auth: Clerk,
      user_id: string,
      body: { public_metadata?: {}; private_metadata?: {}; unsafe_metadata?: {} },
    ) {
      const url = new URL(`https://api.clerk.com/v1/users/${user_id}/metadata`);
    
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.apiKey,
        },
        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