Skip to main content

Fetching Data from the Web within a Lit Action

Overview

Unlike traditional smart contract ecosystems, Lit Actions can natively talk to the external world. This is useful for things like fetching data from the web, or sending API requests to other services.

This Lit Action fetches the current temperature from the National Weather Service API. It will only sign the given message if the temperature forecast exceeds 60°F (15.5°C). By incorporating the API request and response logic directly within the Lit Action, you eliminate the need for a third-party oracle.

Prerequisites

Complete Code Example

The complete code example is available in the Lit Developer Guides Code Repository. There you can find a Node.js and browser implementation of this example code.

Example Lit Action

The signEcdsa function returns a boolean value stored in the sigShare variable. It's true if the message is successfully signed, and false if an error occurs during the Lit Action. If the temperature is below 60°F (15.5°C), the Lit Action will instead return a response message to the user.

const _litActionCode = async () => {
try {
const url = "https://api.weather.gov/gridpoints/TOP/31,80/forecast";
const resp = await fetch(url).then((response) => response.json());
const temp = resp.properties.periods[0].temperature;
console.log("Current temperature from the API:", temp);

if (temp < 60) {
Lit.Actions.setResponse({ response: "It's too cold to sign the message!" });
return;
}

const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName });
Lit.Actions.setResponse({ response: sigShare });
} catch (error) {
Lit.Actions.setResponse({ response: error.message });
}
};

export const litActionCode = `(${_litActionCode.toString()})();`;

Important Considerations

You can use fetch() inside a Lit Action to write data, but caution is necessary. The HTTP request will execute multiple times, once for each Lit Node in the network. When writing data, it's crucial to use operations that produce the same result regardless of how often they're repeated. Consider these examples:

  1. Repeatable operation (preferred):
  • SQL Update: Running it multiple times only changes the row once.
  • Result: Consistent outcome, regardless of repetition.
  1. Non-repeatable operation (avoid):
  • SQL Insert: Each execution creates a new row.
  • Result: Unintended duplicates with multiple executions.

For Lit Actions using fetch() to write data, aim for repeatable operations. This approach prevents issues like duplicate entries or unintended changes if the request repeats due to network conditions or distributed execution across nodes.

Summary

This guide demonstrates how to fetch data from the web within a Lit Action.

If you'd like to learn more about Lit Actions, check out the Lit Actions SDK, or our Advanced Topics section on Lit Actions.

info

Not finding the answer you're looking for? Share your feedback on these docs by creating an issue in our GitHub Issues and Reports repository or get support by visiting our Support page.