5 # This is your API token which can be acquired on the account page.
6 # https://vercel.com/account/tokens
8 # VERCEL_TOKEN="sdfsdfsdfljlbjkljlkjsdfoiwje"
10 VERCEL_API
="https://api.vercel.com"
12 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 _debug fulldomain
"$fulldomain"
17 _debug txtvalue
"$txtvalue"
19 VERCEL_TOKEN
="${VERCEL_TOKEN:-$(_readaccountconf_mutable VERCEL_TOKEN)}"
21 if [ -z "$VERCEL_TOKEN" ]; then
23 _err
"You have not set the Vercel API token yet."
24 _err
"Please visit https://vercel.com/account/tokens to generate it."
28 _saveaccountconf_mutable VERCEL_TOKEN
"$VERCEL_TOKEN"
30 if ! _get_root
"$fulldomain"; then
35 _debug _sub_domain
"$_sub_domain"
36 _debug _domain
"$_domain"
39 if _vercel_rest POST
"v2/domains/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\"}"; then
40 if printf -- "%s" "$response" |
grep "\"uid\":\"" >/dev
/null
; then
44 _err
"Unexpected response while adding text record."
48 _err
"Add txt record error."
55 if ! _get_root
"$fulldomain"; then
60 _vercel_rest GET
"v2/domains/$_domain/records"
62 count
=$
(printf "%s\n" "$response" | _egrep_o
"\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\"" |
wc -l |
tr -d " ")
64 if [ "$count" = "0" ]; then
65 _info
"Don't need to remove."
67 _record_id
=$
(printf "%s" "$response" | _egrep_o
"\"id\":[^,]*,\"slug\":\"[^,]*\",\"name\":\"$_sub_domain\",[^{]*\"type\":\"TXT\",\"value\":\"$txtvalue\"" | cut
-d: -f2 | cut
-d, -f1 |
tr -d '"')
69 if [ "$_record_id" ]; then
70 echo "$_record_id" |
while read -r item
; do
71 if _vercel_rest DELETE
"v2/domains/$_domain/records/$item"; then
72 _info
"removed record" "$item"
75 _err
"failed to remove record" "$item"
83 #################### Private functions below ##################################
84 #_acme-challenge.www.domain.com
86 # _sub_domain=_acme-challenge.www
94 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
100 if ! _vercel_rest GET
"v4/domains/$h"; then
104 if _contains
"$response" "\"name\":\"$h\"" >/dev
/null
; then
105 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
120 path
="$VERCEL_API/$ep"
122 export _H1
="Content-Type: application/json"
123 export _H2
="Authorization: Bearer $VERCEL_TOKEN"
125 if [ "$m" != "GET" ]; then
126 _secure_debug2 data
"$data"
127 response
="$(_post "$data" "$path" "" "$m")"
129 response
="$(_get "$path")"
132 _code
="$(grep "^HTTP
" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r
\\n
")"
133 _debug
"http response code $_code"
134 _secure_debug2 response
"$response"
135 if [ "$_ret" != "0" ]; then
140 response
="$(printf "%s
" "$response" | _normalizeJson)"