There are two approaches: using an Internet service and using some kind of local list (perhaps wrapped in a library). What you want will depend on what you are building.
For services:
For lists:
You could roll your own by downloading the lists from the RIRs:
ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest
The format is documented in this README
1.4m articles
1.4m replys
5 comments
57.0k users