I have a service that allows you to create your own website and then host it for free. Currently its uploading files to Netlify and using their pre-made random urls, but I want to be able to access these newly hosted sites using a subdomain that was created on Namecheap under my main domain e.x thisisthemaindomain.com, i want to access this newly deployed site on Netlify under this url: thisisnewtestsubdomain.thisisthemaindomain.com
maindomain is registered and being hosted somewhere else, but i want to use Namecheap to create the subdomains then Netlify to host and deploy.
def register_subdomain(domain_name, subdomain):
print(domain_name.split('.')[0],domain_name.split('.')[1])
params = {
'ApiUser': api_user,
'ApiKey': api_key,
'UserName': username,
'Command': 'namecheap.domains.ns.create',
'ClientIp': '',
'SLD': domain_name.split('.')[0],
'TLD': domain_name.split('.')[1],
'HostName': subdomain,
'RecordType': 'CNAME',
'Address': main_domain,
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
if 'DomainDNSSetHostsResult IsSuccess="false"' in response.text:
print("Failed to register subdomain. Error:", response.text)
else:
print(f"Subdomain '{subdomain}.{domain_name}' registered successfully.")
else:
print("Failed to connect to the API.")
# Example usage
main_domain = 'thisisthemaindomain.com'
new_subdomain = 'thisisnewtestsubdomain'
register_subdomain(main_domain, new_subdomain)
that returns: Subdomain 'thisisnewtestsubdomain.thisisthemaindomain.com' registered successfully.
so now that i have thisisnewtestsubdomain.thisisthemaindomain.com, I use netlify to create a site:
def create_netlify_site():
url = "https://api.netlify.com/api/v1/sites"
payload = {
"custom_domain": 'thisisnewtestsubdomain.thisisthemaindomain.com',
"force_ssl": False,
"processing_settings": {
"html": { "pretty_urls": False }
}
}
headers = {
"Authorization": "Bearer MY_API_KEY",
"Content-Type": "application/json"
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
print(response)
if response.status_code == 200:
parsed_data = response.json()
print(parsed_data)
else:
print(f"Error: {response} {response.text}")
create_netlify_site()
then i upload the files and deploy the site via netlify api.
what do i need to do to actually make this work and have the site be accessible thru the subdomain url created?
I believe this is what i'm trying to do -> https://docs.netlify.com/domains-https/netlify-dns/delegate-a-subdomain-to-netlify-dns/
would I possibly just need to take the url from the Netlify site I created and then update the dns records for the subdomain on namecheap to something like this?
Record Type: CNAME
Host: thisisnewtestsubdomain
Value: your-site-name.netlify.app
TTL: Default
any pointers would be appreciated thanks, very new to website hosting/DNS congfig. Thanks in advance
While I can't speak to namecheap's API, that part doesn't seem to be your problem. Some suggestions to configure things to work with Netlify:
anything.netlify.app. They serve the site based on the site's configuration of using the hostname, not on the value of the CNAME, and it seems you are setting that right via the Netlify API (custom_domain).PS: you do not need to delegate the subdomain to netlify to do this if you intend to configure each site individually with a hostname and create records at namecheap! you just need individual records at namecheap and to ensure you don't accidentally set up DNS hosting at Netlify which will cause problems with my next comment if you do:
PPS you cannot turn off forcing SSL these days at Netlify so you could skip that config; Netlify will automatically provision an SSL certificate for you as long as you (correctly) configure DNS at namecheap within 3 days of configuring the hostname on a Netlify site. If you wait more than 3 days, you'll have to manually make a change to that custom domain setting, or manually call the SSL renewal/provisioning API: https://open-api.netlify.com/#tag/sniCertificate/operation/provisionSiteTLSCertificate