The Onionoo API offers authoritative information on the state of the Tor Network, including the IP addresses of all relays and the purposes they serve (i.e., guard, exit, etc.). However, JSON-based REST APIs and the Tor Project's exit list aren't the friendliest methods for maintaining firewall rules and other applications that are supposed to consume IP lists.
If you would like a raw list of Tor relays you can visit lists.fissionrelays.net, where you will find a selection of maintained IP address lists for relays that participate in the Tor Network. In that directory, there are several files you can use depending on what you want:
exits.txt: Exit nodes only, IPv4 and IPv6. Use this list to block traffic to your site from the Tor Network.
exits-ipv4.txt: Exit nodes only, IPv4 only.
exits-ipv6.txt: Exit nodes only, IPv6 only.
relays.txt: All relays within the Tor Network, exit and non-exit, IPv4 and IPv6.
relays-ipv4.txt: All relays, IPv4 only.
relays-ipv6.txt: All relays, IPv6 only.
updated.txt: The time in UTC when the list was last updated.
Updated lists will be available at most 10 minutes after the top of every hour. This is to prevent flooding of the Onionoo API. If you would like to make a cron job to fetch the list every hour, here is a good example:
10 * * * * perl -le 'sleep rand 60' && curl -o /path/to/exits.txt https://lists.fissionrelays.net/tor/exits.txt
You could use the Tor Project's bulk exit list, but I found it to be inaccurate, missing IP addresses that are exit nodes and including some that aren't.
As of now, there is no limit to how many times you can request these lists in a certain amount of time. Please exercise good judgement in how often you request these lists, or I may have to start rate limiting. You shouldn't need to query more than once per hour. Alernatively...
The updater script is open source for you to run yourself if you so choose. All you need to run it is vanilla PHP with no other dependencies. You may want to tweak some of the variables near the top, such as
$sleep for best results.