| name | slug | description | framework | useCase | css | deployUrl | demoUrl | relatedTemplates | |
|---|---|---|---|---|---|---|---|---|---|
Domains API |
domains-api |
Learn to leverage Vercel's Domains API to add and remove domains programmatically from your Platforms on Vercel project. |
Next.js |
Documentation |
Tailwind |
|
This code snippet shows you how you can leverage Vercel's Domains API to add and remove domains programmatically from your Platforms on Vercel project.
- Tailwind CSS (
npm install tailwindcss) - SWR (
npm install swr) - React Hot Toast (
npm install react-hot-toast) (optional) - JS Cookie (
npm install js-cookie) (optional)
https://domains-api.vercel.app/
You'll need to configure 3 different environment variables for this project to work:
PROJECT_ID_VERCEL: The ID of the Vercel project you want to add/remove domains from. You can find it under the "Settings" tab in your project's dashboard.TEAM_ID_VERCEL: The ID of the Vercel team you want to add/remove domains from. You can find it under the "Settings" tab in your team's dashboard.AUTH_BEARER_TOKEN: Your personal auth bearer token that gives you programmatic access to your Vercel account. You can create one under the "Tokens" tab in your personal account's settings tab.
To add a domain, you can use the /v8/projects/{idOrName}/domains endpoint as shown here (full documentation).
When a domain is added, there are 3 possible outcomes:
- Domain is added successfully (response code
200). - Domains is already in use by another project and can't be added (response code
409). - Domains belongs to a different team but you can potentially request delegation for the domain (response code
403).
Verifying a project domain can be done with the /v9/projects/{projectId}/domains/{domain}/verify endpoint as shown here (full documentation).
When a domain is first added to a project, we use SWR to periodically check if the domain's DNS records are configured correctly. This is done using the /v6/domains/{domain}/config endpoint as shown here.
There are two ways that your users can configure their domains after they are added:
- CNAME record:
- recommended for subdomains (blog.domain.com, app.domain.com)
- you can set up a branded CNAME value by adding an
Arecord for thecnamesubdomain on your domain and point it to to Vercel's IP address76.76.21.21
- A record:
- recommended for apex domains (domain.com)
To remove a domain, you can use the /v8/projects/{idOrName}/domains endpoint as shown here (full documentation).
You can choose from one of the following two methods to use this repository:
Deploy the example using Vercel:
Execute create-next-app with pnpm to bootstrap the example:
pnpm create next-app --example https://github.com/vercel/examples/tree/main/solutions/domains-api domains-apiNext, run Next.js in development mode:
pnpm devDeploy it to the cloud with Vercel (Documentation).
