Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active May 11, 2023 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/b36054903a6f8460a3d6793c3a2eebb5 to your computer and use it in GitHub Desktop.
Save gilangvperdana/b36054903a6f8460a3d6793c3a2eebb5 to your computer and use it in GitHub Desktop.
Cloudflare API

General

If you need a shortcut or want to develop operational features on Cloudflare, you can take advantage of the Cloudflare API that I have tried (only core functions) with references like my notes.

DNS

CREATE A NEW RECORD

#!/bin/sh
   
EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ZONE_ID="YOURZONEID"; \
TYPE="A"; \
NAME="A.NEW.RECORD.COM"; \
CONTENT="172.20.1.1"; \
PROXIED="false"; \
TTL="1"; \
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/" \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    --data '{"type":"'"$TYPE"'","name":"'"$NAME"'","content":"'"$CONTENT"'","proxied":'"$PROXIED"',"ttl":'"$TTL"'}' \
    | python -m json.tool;

DELETE AN EXISTING RECORD

#!/bin/sh

EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ZONE_ID="YOURZONEID"; \
DNS_ID="DNS_ID"; \
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_ID" \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    | python -m json.tool;

GET ALL EXISTING RECORD

#!/bin/sh


EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ZONE_ID="YOURZONEID"; \
curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    | python -m json.tool;

CLOUDFLARE TUNNEL (ZERO TRUST)

GET ALL CF TUNNEL

#!/bin/sh
EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ACCOUNT_ID="YOURACCOUNT_ID"; \
curl --request GET \
  --url https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    | python -m json.tool;

GET ALL CF TUNNEL CONFIGURATION

#!/bin/sh
EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ACCOUNT_ID="YOURACCOUNT_ID"; \
TUNNEL_ID="YOURTUNNELID"; \
curl --request GET \
  --url https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/configurations \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    | python -m json.tool;

PUT CF TUNNEL CONFIGURATION

#!/bin/sh
EMAIL="yourCF@email.com"; \
KEY="YOURCDAPIKEY"; \
ACCOUNT_ID="YOURACCOUNT_ID"; \
TUNNEL_ID="YOURTUNNELID"; \
curl --request PUT \
  --url https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/configurations \
    -H "X-Auth-Email: $EMAIL" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    --data '{"config":{"ingress":[{"service":"https://172.20.2.40","hostname":"testing.adaptivenetlab.site"},{"service":"http_status:404"}],"warp-routing":{"enabled":false}}}' \
    | python -m json.tool;

JSON to Compact Converter

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment