Overview
Twilio Functions provides a serverless environment for deploying and running code, accessible via auto-generated .twil.io domains. Many developers and businesses want to use a branded custom domain (such as api.yourcompany.com) for their Functions endpoints. This article explains the current options, limitations, and recommended approaches for mapping a custom domain to Twilio Functions.
Using Custom Domains with Twilio Functions
Direct custom domain mapping is not natively supported for Twilio Functions. When you create a Functions service, Twilio automatically assigns a unique .twil.io subdomain (e.g., https://your-service-1234.twil.io). You cannot set a fixed or custom domain directly within Twilio.
What You Need To Know
Recommended Approaches
1. Proxying Through Your Own Domain
To use a branded domain, set up a proxy or redirect from your custom domain to your Twilio Functions endpoint:
-
API Gateway or Reverse Proxy:
Use a service like AWS API Gateway, NGINX, or similar to route requests from your custom domain (e.g.,api.yourcompany.com) to the Twilio Functions URL. -
DNS Configuration:
Configure a CNAME record for your subdomain pointing to your proxy or gateway. -
SSL/HTTPS:
Manage SSL certificates for your custom domain through your proxy or gateway. Twilio automatically provides SSL for.twil.iodomains.
2. Ensuring URL Stability
-
When deploying with the Twilio Serverless Toolkit, use the
--service-nameflag to keep your.twil.iodomain consistent across deployments:twilio serverless:deploy --service-name=your-service-name
- As long as you deploy to the same service name, the
.twil.iodomain remains unchanged.
Limitations
-
Root Domain Mapping:
You cannot map the root domain (e.g.,yourcompany.com) directly to Twilio Functions. Use a subdomain (e.g.,api.yourcompany.com). -
No Native Custom Domain Support:
Twilio does not currently allow direct custom domain assignment or management for Functions.
Example Workflow
-
Create a Twilio Functions Service
Deploy your code and note the.twil.iodomain assigned. -
Set Up a Proxy
Configure your API Gateway or reverse proxy to forward requests from your custom domain to the Twilio Functions endpoint. -
Update DNS
Add a CNAME record for your subdomain pointing to your proxy. -
Manage SSL
Ensure SSL certificates are in place for your custom domain.
Conclusion
While Twilio Functions does not natively support custom domains, you can achieve a branded URL by proxying requests through your own infrastructure. This approach gives you control over your public-facing domain and ensures URL stability, even if the underlying Twilio domain changes.
Below you will find references to useful documents:
- Twilio Serverless Toolkit Documentation
- Stack Overflow Discussion: Twilio Serverless Function URL Custom Domain
For further questions or guidance, consult Twilio documentation or reach out to your support contact.