LogoLogo
WebsiteDiscordTelegramTwitter
  • 🏠Witnet Oracle Docs
  • Introduction
    • ❓What is Witnet?
      • ⚙️Oracle Architecture
      • 🪙The Witnet Coin (Wit)
      • 👛Wallets
      • 🤔Frequently Asked Questions
      • 😎Awesome Witnet
    • 🚀Quick Tutorials
      • 📈Price Feeds
      • 🎲Randomness
      • 🌐APIs and HTTP GET/POST
      • ⛏️Mining Wit
  • Smart contracts developers
    • ⛓️Supported chains
    • 🔮Wit/Oracle
      • HTTP GET Requests in Solidity
      • HTTP POST Requests in Solidity
      • Query GraphQL APIs in Solidity
      • Dynamic Requests in Solidity
      • UsingWitnet Inheritance
      • API Reference
        • Solidity API
          • Solidity Contracts
            • WitnetRequestBoard
        • Javascript API
        • RADON API
          • RADON Type System
          • RADON Errors
      • Multi-chain Addresses
    • 💹Wit/Price Feeds
      • How To Use Witnet Price Feeds
      • Update Conditions
      • Code Examples
      • API Reference
      • Multi-chain Addresses
        • Arbitrum Price Feeds
        • Avalanche Price Feeds
        • Base Price Feeds
        • Boba Price Feeds
        • Celo Price Feeds
        • Conflux Price Feeds
        • Cronos Price Feeds
        • Dogechain Price Feeds
        • Elastos Price Feeds
        • Ethereum Price Feeds
        • Gnosis Chain Price Feeds
        • Kaia Price Feeds
        • Kava Price Feeds
        • KCC Price Feeds
        • Mantle Price Feeds
        • Meter Price Feeds
        • Metis Price Feeds
        • Moonbeam Price Feeds
        • OKX Price Feeds
        • Optimism Price Feeds
        • Polygon Price Feeds
        • Reef Price Feeds
        • Scroll Price Feeds
        • Syscoin Price Feeds
        • Ultron Price Feeds
        • Request new price feed or chain support
    • 🎲Wit/Randomness
      • Generating Randomness
      • WitnetRandomness Contract
      • Low-level Requests
      • Code Examples
      • API Reference
      • Multi-chain Addresses
    • 🏗️Guides
      • 📖Solidity Contracts
        • Appliances
          • 📃WitnetPriceFeeds
          • 📃WitnetRandomness
        • Core
          • 📃WitnetOracle
          • 📃WitnetRadonRegistry
          • 📃WitnetRequest
          • 📃WitnetRequestFactory
          • 📃WitnetRequestTemplate
        • Mockups
          • 📃UsingWitnet
          • 📃UsingWitnetRandomness
          • 📃UsingWitnetRequest
          • 📃UsingWitnetRequestTemplate
          • 📃WitnetRandomnessRequestConsumer
          • 📃WitnetRequestConsumer
          • 📃WitnetRequestTemplateConsumer
      • 🧙Solidity Wizard
    • 🎓Tutorials
      • Building a Satoshi/Wei custom price feed
  • Witnet Node Operators
    • 🖥️Requirements
    • 🚀Witnet Node Quick Start Guide (Docker)
    • 🔎Next steps
    • 📄CLI Reference
    • 🤓Advanced Setups
      • Run Witnet as a systemd service
      • Run Witnet as a docker compose service
      • Paranoid mode (Witnet over proxies and Tor)
      • Configuration file
  • Witnet Node Developers
    • 🏗️Integration Guide
      • Node API Reference
      • Wallet API Reference
    • 🗜️Compile witnet-rust from Source Code
Powered by GitBook
On this page
  • Performing HTTP GET queries right from your Solidity smart contracts
  • Learn More About The Witnet HTTP GET/POST Oracle

Was this helpful?

Edit on GitHub
  1. Introduction
  2. Quick Tutorials

APIs and HTTP GET/POST

PreviousRandomnessNextMining Wit

Last updated 2 years ago

Was this helpful?

One of the core capabilities of the Witnet protocol is to enable smart contracts to securely retrieve data from conventional APIs through HTTP(S) GET and POST calls.

This is specially convenient for pieces of data that are available from multiple sources, because many of them can be queried at once and aggregated together, thus getting an increased level of decentralization and fault tolerance.

Performing HTTP GET queries right from your Solidity smart contracts

Project setup

The HTTP GET/POST capability is enabled by the , a template project that includes everything you need to quickly get up to speed:

mkdir my-first-witnet-project
cd my-first-witnet-project
truffle unbox witnet/truffle-box

Data request file

HTTP GET/POST queries are defined using a Javascript based DSL and stored in the requests folder of your repository.

This exemplifies a data request that fetches the BTC/USD price from 2 different REST APIs:

import * as Witnet from "witnet-requests"

const bitstamp = new Witnet.Source("https://www.bitstamp.net/api/ticker/")
  .parseJSONMap()
  .getFloat("last")

const coindesk = new Witnet.Source("https://api.coindesk.com/v1/bpi/currentprice.json")
  .parseJSONMap()
  .getMap("bpi")
  .getMap("USD")
  .getFloat("rate_float")

const aggregator = new Witnet.Aggregator({
  filters: [
   [Witnet.Types.FILTERS.deviationStandard, 1.5]
  ],
  reducer: Witnet.Types.REDUCERS.averageMean
})

const tally = new Witnet.Tally({
  filters: [
   [Witnet.Types.FILTERS.deviationStandard, 1.5]
  ],
  reducer: Witnet.Types.REDUCERS.averageMean
})

const request = new Witnet.Request()
  .addSource(bitstamp)
  .addSource(coindesk)
  .setAggregator(aggregator)
  .setTally(tally)
  .setQuorum(25)
  .setFees(1000000, 1000000)
  .setCollateral(10000000000)

export { request as default }

Learn More About The Witnet HTTP GET/POST Oracle

You can find here the complete documentation and API reference for the Witnet HTTP GET/POST oracle, along many more examples and walkthroughs:

Want to make sense of all the Javascript above? Please make sure to check out the , where every step is explained in detail

🚀
🌐
Witnet Truffle Box
Truffle
full HTTP GET tutorial
🔮Wit/Oracle