Why are Dart http requests for GeminiAPI on Ubuntu VPS being blocked

I am running a simple Dart server with package shelf. The server makes a request to generativelanguage.googleapis.com. Everything is working fine on my own machine. When the Dart program is running on a Ubuntu VPS located in Germany, the API is returning { "error": { "code": 400, "message": "User location is not supported for the API use.", "status": "FAILED_PRECONDITION" } } The VPS is actually in Germany. The Gemini API is supported in Germany. Using tools to find the location by the IP shows Germany. If I use curl from the VPS, the response is normal, I get the data. If I do Process.run in Dart and call curl, it works. If I use dart:http, google_generative_ai package from pub.dev, dio, custom http clients, it returns unsupported user location. If I run the equivalent code in a python flask server on the VPS, it works.

I suspect there is something wrong in the internals of the dart:http package?

I have tried creating custom Http clients and setting every header possible, I’ve tried multiple User-Agents, I’ve tried it with ssl and no ssl, I’ve tried cors headers, I’ve tried dockerizing the dart server on my local machine and running it on the VPS, I’ve tried analyzing the network traffic and it is indeed coming from the correct IP coming from Germany, I’ve tried running the server on different ports, I’ve tried running the server behind nginx, I’ve tried running the server on WSL and it works, I’ve tried requesting my IP from the Dart application and printing it, I’ve tried HTTP requests to other APIs from the Dart app and they work fine, I’ve verified that the API key is correct inside Dart (if I append ‘asd’ for example I get the correct error back about the API key being invalid).

Here are a few solutions:
Apparently, a lot of iPv4 addresses are blocked, because they were used maliciously in the past and not all of them have been reevaluated everywhere yet.

Use iPv6 to connect to generativelanguage.googleapis.com, that way you bypass your iPv4 being blocked.

Use a proxy and iPv4, that way your iPv4 block is circumvented. (eg. running a squid proxy, and telling your http client to go through it)

The main problem here is that the Dart SDK always FAVORS looking up iPv4 first, so a dart server making requests to the Gemini API will always be declined (if it is running on a vps with its IP blocked by Google). You have to use a custom http client that uses only iPv6 internet address lookup manually.

Linking the related github issue: dart:io does not use ipv6 on ubuntu 24.04 unless forced · Issue #60192 · dart-lang/sdk · GitHub

1 Like