Cloudflare PUT API only keeps last subdomain reddit / selfhosted ---------------------------------------------------------------------- Mixo-Max · 2023-12-09 23:05 i wrote this Cloudflare dns updater in python: def update_subdomain(subdomain, ext_ip, proxied=True): zone_id = get_zone_id() #correct identifier = get_identifier(subdomain) #correct url = f"https://api.cloudflare.com/client/v4/zones/{zone_id}/d ns_records/{identifier}" #correct url payload = { "content": ext_ip, "name": subdomain, "type": "A", "comment": "Automatically updated by Cloudflare.py", "ttl": 1, "proxied": proxied } headers = get_headers() #correct auth headers response = requests.request("PUT", url, json=payload, headers=headers) print(response.json()) # -> {'result': {'id': 'XXX', 'zone_id': 'XXX', 'zone_name': 'XXX', 'name': 'XXX', 'type': 'A', 'content': 'XXX', 'proxiable': True, 'proxied': False, 'ttl': 1, 'locked': False, 'meta': {'auto_added': False, 'managed_by_apps': False, 'managed_by_argo_tunnel': False, 'source': 'primary'}, 'comment': 'Automatically updated by Cloudflare.py', 'tags': [], 'created_on': '2023-10-09T08:12:39.52179Z', 'modified_on': '2023-12-09T22:46:19.848655Z'}, 'success': True, 'errors': [], 'messages': []} return response.ok in the response it says that it updated the subdomain and i can see it in the records but when i call the function again with a different subdomain but the same external ip, it deletes the previously created record. ---------------------------------------------------------------------- thekrautboy · 2023-12-09 23:10 /r/Cloudflare? ---------------------------------------------------------------------- latkde · 2023-12-09 23:23 A PUT request *overwrites* or *replaces* the contents of a resource. Here, the resource is the Cloudflare DNS entry with that `identifier`. Think of this identifier as a kind of "line number" in a hosts file. I suspect that you are using the same identifier for all subdomains, therefore overwriting the same DNS entry with data for another subdomain. * maybe you can find the correct identifier for the DNS entry that you want to overwrite? * maybe you can POST to the `dns_records` endpoint to create a new entry? ---------------------------------------------------------------------- Mixo-Max · 2023-12-09 23:23 i also posted there but r/CloudFlare is significantly smaller (currently 68 online vs 1.3k) so the chanche to get a reply is lower there ---------------------------------------------------------------------- thekrautboy · 2023-12-09 23:24 Good luck... ---------------------------------------------------------------------- Mixo-Max · 2023-12-09 23:25 Thanks, i did not know that. when i just change "PUT" to "POST", i get: {'code': 10000, 'message': 'POST method not allowed for the api\_token authentication scheme'} how do i need to change my request headers to properly post? ---------------------------------------------------------------------- pytho38 · 2023-12-09 23:37 Not sure how you are planning to use this, but the cloudflare terraform provider is solid and I use it to maintain my records. ---------------------------------------------------------------------- TheoR700 · 2023-12-09 23:43 https://developers.cloudflare.com/api/ ---------------------------------------------------------------------- rockthescrote · 2023-12-10 14:00 If you _only_ change PUT->POST then you’re trying to “add” a new record to `…zones/{zoneid}/dns_records/{someExisingRecordId}`. You can’t add a record to a record. You want to add a record to _the set of records_, i.e `POST …. zones/{zoneid}/dns_records` ---------------------------------------------------------------------- Mixo-Max · 2023-12-10 16:34 Thank you, i fixed it. the problem was with my get\_identifier() function that always returned the identifier of the root ([example.com](https://example.com)) instead of ([subdomain.example.com](https://subdomain.example.com))