0

guys, I'm puzzled at how the onchain program accessing external APIs. For example, calling REST API by some LLM model from onchain program or access github REST APIs...

And I didn't see such examples in Anchor or Seahorse. (Just use Rust REST client for that?)

I don't understand.

2 Answers 2

2

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.

1
  • Oh, thank you. Well, I feel that such restrictions hinder the use of blockchain and the integration of many applications. After all, there are still many external services and applications. However, if external applications collect and store data on the blockchain, it cannot be guaranteed that this is a contract.
    – Yarco
    Commented Oct 30 at 7:58
1

As far I know, There no way to fetch , or call Rest API or any other external apis in a solana program , Cause when executing any instructions , before the execution all the states of the accounts are fetched.

1
  • Thank you. It sounds like "External App => solana" is the only way for now.
    – Yarco
    Commented Oct 30 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.