Connect Smart Contracts to Web2 API with Gelato Web3 Functions
Gelato Team
•
Aug 4, 2023

Gelato Web3 Functions offer a powerful solution to connect on-chain smart contracts with off-chain data. The on-chain component is the smart contract you want to automate, while the off-chain element is the Web3 Function that fetches and processes external API or subgraph data.
By configuring a Web3 Function Task, you can seamlessly integrate both sides, enabling greater control and automation for decentralized applications. In this blog post, we demonstrate 5 ways to integrate Web2 APIs with smart contracts, complete with code examples.
1. Update Oracle Smart Contract using CoinGecko Price API
CoinGecko provides real-time price feeds and crypto market data. Using Web3 Functions, you can fetch the price of ETH from CoinGecko and update an oracle contract on-chain every 5 minutes.
Implementation
Example Web3 Function script to update an oracle with CoinGecko ETH price:
2. Automated Trades with Web3 Functions
Automated trading strategies like Trailing Stop or Bounce Entry can be implemented by monitoring Uniswap prices via Web3 Functions.
Implementation
Trailing Stop: Exit a trade if the price drops below a dynamic threshold from the max recorded price.
Bounce Entry: Enter a trade if the price increases beyond a threshold from the min recorded price.
See the GitHub code for full strategy implementations.
3. Sports Betting Platform
A betting contract can use Web3 Functions to fetch match results from a sports API and trigger on-chain payouts automatically.
Implementation
4. Automated Content Creation on Lens with ChatGPT
Combine AI and Web3 by using Web3 Functions to connect with the OpenAI ChatGPT API and post automated content on Lens Protocol.
Implementation
The LensGelatoGPT contract manages prompts, while the Web3 Function calls OpenAI, validates metadata, uploads it to IPFS, and triggers LensHub’s post
function.
See the GitHub tutorial for complete code.
5. NFT Creation with Stable Diffusion API
Use Stable Diffusion to generate NFT artwork dynamically and assign images as metadata to unrevealed NFTs.
Implementation
See the GitHub repo for full details.
About Gelato
Join our community on Discord.
Web3 Functions are available today in private beta. Explore the docs and our in-depth tutorial. Apply here to test Web3 Functions.
If you’re new to Gelato
Gelato is Web3’s decentralized backend powering automated, gasless, and off-chain aware smart contracts across Ethereum, Arbitrum, Polygon, Optimism, zkSync, and more. 400+ projects rely on Gelato for millions of transactions in DeFi, NFTs, and gaming.
Core Services:
Web3 Functions: Connect smart contracts to off-chain data & computation.
Automate: Execute transactions automatically with reliability.
Relay: Provide gasless transactions with a simple API.
Gasless Wallet: An SDK combining Gelato Relay + Safe’s Smart Wallet to enable Account Abstraction.
Subscribe to our newsletter and turn on Twitter notifications for updates.
Explore open positions and apply here.