On-Chain Programs Can't Directly Access External APIs: On Solana (and most other blockchains), programs can’t call external APIs directly due to the trust and decentralisation model. They run in a "sandboxed" environment with no internet access or capability to fetch external data on their own.
How External Data Gets On-Chain: The usual way to get off-chain data onto the blockchain is through oracles. Oracles are specialised services that retrieve data from the external world (such as API responses) and post it onto the blockchain. Chainlink, for example, is a popular oracle service that provides off-chain data to various blockchains, including Solana.
Rust REST Clients in Anchor or Seahorse: Rust libraries for REST clients exist, but they are used in off-chain environments. If you're using Anchor or Seahorse for Solana development, you would typically set up an off-chain client (like a server or script) that gathers data from external APIs and interacts with the on-chain program by sending transactions. These transactions can contain data from the external API, which the program can then process.
Using Oracles with Anchor and Seahorse: If you need specific external data (such as price feeds, weather data, or GitHub statistics) on-chain, you could either set up an oracle or integrate with an existing oracle service. Some developers also set up their off-chain servers to periodically call external APIs, package the results, and send them to the on-chain program.
LLM or GitHub APIs: If you need data from an LLM or GitHub API, a common approach would be:
Use an off-chain server or script to call these APIs.
Take the results, structure them into a transaction, and send this transaction to the on-chain program.
The on-chain program can then process the data as needed, store it, or perform computations.
In summary, while you can’t call external APIs directly from an on-chain program, the common approach is to handle API calls off-chain and send the resulting data on-chain through transactions or oracles.