Configure tunnel endpoints·续
</details>
Note
You will need your account ID and API token to use the API.
<details>
<summary>
GRE tunnel
</summary>
Create a <code>POST</code> request <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/gre_tunnels/methods/create/">using the API</a> to create a GRE tunnel.
<details>
<summary>
Required API token permissions
</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Magic WAN Write</code>
- <code>Magic Transit Write</code>
</details>
Create a GRE tunnelbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/gre_tunnels" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "<TUNNEL_NAME>",
"description": "<TUNNEL_DESCRIPTION>",
"interface_address": "<INTERFACE_ADDRESS>",
"cloudflare_gre_endpoint": "<CLOUDFLARE_ENDPOINT>",
"customer_gre_endpoint": "<CUSTOMER_ENDPOINT>"
}'
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"gre_tunnels": [
{
"cloudflare_gre_endpoint": "<IP_ADDRESS>",
"customer_gre_endpoint": "<IP_ADDRESS>",
"interface_address": "<INTERFACE_CIDR>",
"name": "<TUNNEL_NAME>",
"description": "<TUNNEL_DESCRIPTION>",
"health_check": {
"direction": "unidirectional",
"enabled": true,
"rate": "low",
"type": "reply"
},
"mtu": 0,
"ttl": 0
}
]
},
"success": true
}
</details>
<details>
<summary>
IPsec tunnel
</summary>
- Create a <code>POST</code> request <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/create/">using the API</a> to create an IPsec tunnel.
Note that in the example, replay protection is disabled by default. You can enable it with the flag <code>"replay_protection": true</code> for each IPsec tunnel, if the devices you use do not support disabling this feature. If you have already created IPsec tunnels, update them with a <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/update/"><code>PUT</code> request</a>. Refer to <a href="https://developers.cloudflare.com/cloudflare-wan/reference/anti-replay-protection/">Anti-replay protection</a> for more information on this topic.<details><summary>
Required API token permissions</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required: - <code>Magic WAN Write</code> - <code>Magic Transit Write</code></details>
Create an IPsec tunnelbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/ipsec_tunnels" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "<TUNNEL_NAME>",
"description": "<TUNNEL_DESCRIPTION>",
"interface_address": "<INTERFACE_ADDRESS>",
"cloudflare_endpoint": "<CLOUDFLARE_ENDPOINT>",
"customer_endpoint": "<CUSTOMER_ENDPOINT>"
}'
```
```json
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"ipsec_tunnels": [
{
"id": "<IPSEC_TUNNEL_ID>",
"interface_address": "<INTERFACE_CIDR>",
"name": "<TUNNEL_NAME>",
"cloudflare_endpoint": "<IP_ADDRESS>",
"customer_endpoint": "<IP_ADDRESS>",
"description": "<TUNNEL_DESCRIPTION>",
"health_check": {
"direction": "unidirectional",
"enabled": true,
"rate": "low",
"type": "reply"
},
"psk_metadata": {},
"replay_protection": false
}
]
},
"success": true
}
```
Take note of the tunnel <code>id</code> value. We will use it to generate a pre-shared key (PSK).
2. Create a <code>POST</code> <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/psk_generate/">request</a> to generate a PSK. Use the tunnel <code>id</code> value you received from the previous command.<details><summary>
Required API token permissions</summary>
At least one of the following <a href="https://developers.cloudflare.com/fundamentals/api/reference/permissions/">token permissions</a> is required:
- <code>Magic WAN Write</code>
- <code>Magic Transit Write</code></details>
*Generate Pre-Shared Key (PSK) for IPsec tunnelsbash*
```bash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/magic/ipsec_tunnels/$IPSEC_TUNNEL_ID/psk_generate" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
```
```json
{
"result": {
"ipsec_id": "<IPSEC_ID>",
"ipsec_tunnel_id": "<IPSEC_TUNNEL_ID>",
"psk": "<PSK_CODE>",
"psk_metadata": {
"last_generated_on": "2025-03-13T14:28:47.054317925Z"
}
},
"success": true,
"errors": [],
"messages": []
}
```
Take note of your <code>psk</code> value.
3. Create a <code>PUT</code> <a href="https://developers.cloudflare.com/api/resources/magic_transit/subresources/ipsec_tunnels/methods/update/">request</a> to update your IPsec tunnel with the PSK.
```bash
curl "https://api.cloudflare.com/client/v4/accounts/%7Baccount_id%7D/magic/ipsec_tunnels/%7Bipsec_tunnel_id%7D" \
--request PUT \
--json '{
"psk": "<PSK_VALUE>"
}'
```
```json
{
"result": {
"modified": true,
"modified_ipsec_tunnel": {
"id": "<IPSEC_ID>",
"interface_address": "<IPSEC_CIDR>",
"created_on": "2025-03-13T14:28:21.139535Z",
"modified_on": "2025-03-13T14:33:26.09683Z",
"name": "<TUNNEL_NAME>",
"cloudflare_endpoint": "<IP_ADDRESS>",
"customer_endpoint": "<IP_ADDRESS>",
"remote_identities": {
"hex_id": "",
"fqdn_id": "",
"user_id": ""
},
"psk_metadata": {
"last_generated_on": "2025-03-13T14:28:47.054318Z"
},
"description": "<TUNNEL_DESCRIPTION>",
"health_check": {
"enabled": true,
"target": "",
"type": "reply",
"rate": "mid",
"direction": "unidirectional"
}
}
},
"success": true,
"errors": [],
"messages": []
}
- Use the <code>psk</code> value from step 3 to configure the IPsec tunnel on your equipment as well.
</details>
<details>
<summary>
Configure bidirectional health checks
</summary>
Bidirectional health checks are available for GRE and IPsec tunnels. For Cloudflare WAN this option defaults to bidirectional.
You can change this setting via the API with <code>"bidirectional"</code> or <code>"unidirectional"</code>:
curl "https://api.cloudflare.com/client/v4/accounts/%7Baccount_id%7D/magic/ipsec_tunnels/%7Bipsec_tunnel_id%7D" \
--request PUT \
--json '{
"health_check": {
"direction": "bidirectional"
}
}'
{
"result": {
"modified": true,
"modified_ipsec_tunnel": {
"id": "<IPSEC_ID>",
"interface_address": "<IPSEC_CIDR>",
"created_on": "2025-03-13T14:28:21.139535Z",
"modified_on": "2025-03-13T14:33:26.09683Z",
"name": "<TUNNEL_NAME>",
"cloudflare_endpoint": "<IP_ADDRESS>",
"customer_endpoint": "<IP_ADDRESS>",
"remote_identities": {
"hex_id": "",
"fqdn_id": "",
"user_id": ""
},
"psk_metadata": {
"last_generated_on": "2025-03-13T14:28:47.054318Z"
},
"description": "<TUNNEL_DESCRIPTION>",
"health_check": {
"enabled": true,
"target": "",
"type": "reply",
"rate": "mid",
"direction": "bidirectional"
}
}
},
"success": true,
"errors": [],
"messages": []
}
</details>
Bidirectional vs unidirectional health checks
To check for tunnel health, Cloudflare sends a health check probe consisting of ICMP (Internet Control Message Protocol) reply packets ↗︎ to your network. Cloudflare needs to receive these probes to know if your tunnel is healthy.
Cloudflare defaults to bidirectional health checks for Cloudflare WAN, and unidirectional health checks for Magic Transit (direct server return). However, routing unidirectional ICMP reply packets over the Internet to Cloudflare is sometimes subject to drops by intermediate network devices, such as stateful firewalls. Magic Transit customers with egress traffic can modify this setting to bidirectional.
Legacy bidirectional health checks
For customers using the legacy health check system with a public IP range, Cloudflare recommends:
- Configuring the tunnel health check target IP address to one within the
172.64.240.252/30prefix range. - Applying a policy-based route that matches packets ↗︎ with a source IP address equal to the configured tunnel health check target (for example
172.64.240.253/32), and route them over the tunnel back to Cloudflare.
Next steps
Now that you have set up your tunnel endpoints, you need to configure routes to direct your traffic through Cloudflare. You have two routing options:
- Static routes: Best for simple, stable networks where routes rarely change. You manually define each route.
- BGP peering: Best for dynamic environments with frequently changing routes, multiple prefixes, or when you need automatic failover. Requires enabling BGP on your tunnel during creation.
Refer to Configure routes for detailed instructions on both options.
After configuring your routes, you need to set up a site.
Troubleshooting
If you experience issues with your tunnels:
- For tunnel health check problems, refer to Troubleshoot tunnel health.
- For IPsec tunnel establishment issues, refer to Troubleshoot with IPsec logs.
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/cloudflare-wan/configuration/how-to/configure-tunnel-endpoints/#page","headline":"Configure tunnel endpoints","description":"Learn how to configure IPsec or GRE tunnels for Cloudflare WAN.","url":"https://developers.cloudflare.com/cloudflare-wan/configuration/how-to/configure-tunnel-endpoints/","inLanguage":"en","image":"https://developers.cloudflare.com/cloudflare-wan/configuration/how-to/configure-tunnel-endpoints/og.png?v=d3c061b9d5f16e47","dateModified":"2026-10-06","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Learn what settings you need to change to perform a useful traceroute to an endpoint behind a Cloudflare Tunnel.
title: Run traceroute
image: https://developers.cloudflare.com/cloudflare-wan/configuration/how-to/traceroute/og.png?v=c89b4f0470f74986
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cloudflare-wan/llms.txt
Use this file to discover all available pages before exploring further.
Docs