When using Internet API interfaces, if the client network supports both IPv4 and IPv6, some operating systems or network environments may attempt to connect to the server through IPv6 first. If the IPv6 network has unstable routing, connection timeouts, poor carrier network quality, or other issues, API requests may become slow, time out, or even fail completely.
This article uses BYW API as an example to explain how to make API requests prefer IPv4 on Windows, Linux, macOS, and common development environments.
1. Why Should You Prefer IPv4?
Modern operating systems generally support both IPv4 and IPv6. When an API domain has both an A record and an AAAA record, the client may receive both IPv4 and IPv6 addresses.
For example:
| DNS Type | Example | Protocol |
|---|---|---|
| A | 203.0.113.10 | IPv4 |
| AAAA | 2001:db8::10 | IPv6 |
If the client attempts to use IPv6 first while the current network has poor IPv6 connectivity, the following problems may occur when accessing the API:
- Long API connection times
- HTTPS connection timeouts
- DNS resolution works normally, but the API cannot be accessed
- IPv4 works, while IPv6 cannot connect
- The API works normally in some regions but fails or becomes unstable in others
2. First Confirm That IPv4 Access Works Normally
Before changing your operating system configuration, it is recommended to use curl to test the API over both IPv4 and IPv6.
Force IPv4
curl -4 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
Force IPv6
curl -6 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
If you get one of the following results:
| IPv4 | IPv6 | Description |
|---|---|---|
| Normal | Normal | Both IPv4 and IPv6 are available |
| Normal | Timeout | IPv4 is recommended |
| Normal | Unable to connect | IPv4 is recommended |
| Abnormal | Abnormal | Check the local network or API address |
3. Configure Windows to Prefer IPv4
Windows supports IPv4 and IPv6 dual-stack networking by default. If you want Windows to prefer IPv4 for network communication, you can adjust the IPv6 address selection policy.
Method 1: Set IPv4 as the Preferred Protocol Using a Command
Open CMD as Administrator and run:
netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 100 4
After applying the setting, you can use the following command to view the current IPv6 prefix policy:
netsh interface ipv6 show prefixpolicies
If you see a configuration similar to the following, the priority of IPv4-mapped addresses has been increased:
Precedence Label Prefix ---------- ----- ---------------- 100 4 ::ffff:0:0/96
Method 2: Disable IPv6 Directly on Windows
If the customer confirms that IPv6 is completely unnecessary in the current network environment, IPv6 can also be disabled in the network adapter settings.
- Open the Windows Control Panel
- Go to Network and Internet
- Open the Network and Sharing Center
- Click the currently active network connection
- Open Properties
- Find Internet Protocol Version 6 (TCP/IPv6)
- Uncheck IPv6
- Save the settings
4. Configure Linux to Prefer IPv4
Linux can adjust its address selection policy through /etc/gai.conf.
Edit gai.conf
sudo nano /etc/gai.conf
Find a configuration similar to the following:
#precedence ::ffff:0:0/96 100
Remove the # at the beginning and change it to:
precedence ::ffff:0:0/96 100
After saving the configuration, Linux will give IPv4 addresses a higher selection priority when operating in an IPv4/IPv6 dual-stack environment.
Test API Access over IPv4 on Linux
curl -4 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
Test API Access over IPv6 on Linux
curl -6 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
5. How to Prefer IPv4 on macOS
macOS also supports IPv4 and IPv6 dual-stack networking. For ordinary API users, it is generally not recommended to modify the entire system's IPv6 configuration just for one API.
If you only want to test whether the BYW API can be accessed normally over IPv4, you can use curl directly:
curl -4 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
If IPv4 requests work normally while requests without specifying a protocol time out, you can further check the current macOS network environment and the HTTP library used by the specific API client.
6. How Developers Can Make Their Applications Use IPv4
If the API is accessed by an application, it is generally recommended to control the network protocol at the client application level rather than modifying the customer's operating system. This avoids affecting other websites and applications on the customer's computer.
C# / .NET
If you are developing an API client using C# / .NET, you can control the connection method according to the HTTP Handler being used. For scenarios that require strict IPv4 usage, you can connect using an IPv4 address or filter DNS resolution results to IPv4 addresses at the network layer.
If you are simply troubleshooting an API network issue, first use curl to perform a comparison test:
curl -4 "https://api.byw.bet/api?token=YOUR_TOKEN&t=130&p=json"
If the curl IPv4 request works normally while the application request fails, you should further check the HTTP Handler, DNS resolution, proxy configuration, connection timeout settings, and connection strategy used by the application.
Other Programming Languages
Development environments such as Java, Go, Python, PHP, and Node.js can also control the address family through their HTTP clients or Socket layer. If the client needs to force IPv4, it is preferable to handle this at the application level rather than requiring the customer to disable IPv6 across the entire operating system.
7. How to Determine Whether the API Is Using IPv4 or IPv6
You can use DNS query tools to check whether the API domain has both A and AAAA records.
Linux / macOS
dig A api.byw.bet
dig AAAA api.byw.bet
Windows
nslookup -type=A api.byw.bet
nslookup -type=AAAA api.byw.bet
If an A record exists, the domain provides an IPv4 address. If an AAAA record exists, the domain provides an IPv6 address.
8. Recommended Solutions
| Scenario | Recommended Solution |
|---|---|
| Ordinary API users | Prefer IPv4; there is no need to disable IPv6 |
| Windows users | Adjust the IPv4/IPv6 address selection priority |
| Linux users | Modify /etc/gai.conf |
| macOS users | Prefer specifying IPv4 in the API client |
| Developers | Control IPv4 at the HTTP client or network layer |
| Network troubleshooting | Use curl -4 and curl -6 for separate tests |
| IPv6 is completely unavailable | Consider disabling IPv6 only when necessary |
9. Frequently Asked Questions
1. Will using IPv4 affect API data?
No. IPv4 and IPv6 are simply network communication protocols used between the client and server. As long as the client ultimately connects to the same API service, the returned data will not change based on whether IPv4 or IPv6 is used.
2. Is IPv4 always faster than IPv6?
Not necessarily. IPv6 may provide better connectivity in some network environments. Therefore, the recommendation to prefer IPv4 mainly applies to situations where IPv6 connectivity has poor quality, is unstable, or cannot access the API normally.
3. Can disabling IPv6 solve all API timeout problems?
No. API timeouts can also be caused by DNS, network routing, firewalls, proxy servers, server load, HTTPS/TLS connections, and client-side timeout settings.
4. What if curl -4 works but the application still fails?
This usually indicates that the server's IPv4 network is working normally. You should focus on checking the HTTP client, DNS resolution, proxy settings, connection timeout, and IPv6/IPv4 address selection strategy used by the application.
10. Summary
If a client experiences IPv6 connection timeouts when accessing the BYW API while IPv4 works normally, the recommended approach is:
- First use
curl -4to test the API. - Confirm that the API can be accessed normally over IPv4.
- If IPv6 has connectivity issues, configure the client to prefer IPv4.
- Developers can control IPv4 usage at the application network layer.
- Unless absolutely necessary, do not disable IPv6 across the entire operating system.