0

Get an email address for current user

by
Published Oct 24, 2023

Returns details about a specific one of the authenticated user's email addresses. Details describe whether the address has been confirmed by the user and whether it is the user's primary address or not.

Script bitbucket Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 399 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get an email address for current user
7
 * Returns details about a specific one of the authenticated user's
8
email addresses.
9

10
Details describe whether the address has been confirmed by the user and
11
whether it is the user's primary address or not.
12
 */
13
export async function main(auth: Bitbucket, email: string) {
14
  const url = new URL(`https://api.bitbucket.org/2.0/user/emails/${email}`);
15

16
  const response = await fetch(url, {
17
    method: "GET",
18
    headers: {
19
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
20
    },
21
    body: undefined,
22
  });
23
  if (!response.ok) {
24
    const text = await response.text();
25
    throw new Error(`${response.status} ${text}`);
26
  }
27
  return await response.json();
28
}
29