1

Follow a user

by
Published Oct 25, 2023

Note that you'll need to set `Content-Length` to zero when calling out to this endpoint. For more information, see "[HTTP verbs](https://docs.github.com/rest/overview/resources-in-the-rest-api#http-verbs)." Following a user requires the user to be logged in and authenticated with basic auth or OAuth with the `user:follow` scope.

Script github Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Follow a user
6
 * Note that you'll need to set `Content-Length` to zero when calling out to this endpoint. For more information, see "[HTTP verbs](https://docs.github.com/rest/overview/resources-in-the-rest-api#http-verbs)."
7

8
Following a user requires the user to be logged in and authenticated with basic auth or OAuth with the `user:follow` scope.
9
 */
10
export async function main(auth: Github, username: string) {
11
  const url = new URL(`https://api.github.com/user/following/${username}`);
12

13
  const response = await fetch(url, {
14
    method: "PUT",
15
    headers: {
16
      Authorization: "Bearer " + auth.token,
17
    },
18
    body: undefined,
19
  });
20
  if (!response.ok) {
21
    const text = await response.text();
22
    throw new Error(`${response.status} ${text}`);
23
  }
24
  return await response.text();
25
}
26