Book a free call

How to Implement a Blockchain Oracle Step by Step

How to implement a blockchain oracle: pick a data source, connect the feed to your contract, add freshness and safety checks, test failures, then monitor it live.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How to Implement a Blockchain Oracle Step by Step
On this page

Short answer

A blockchain oracle is implemented by defining the exact data your smart contract needs, connecting a trusted feed to the contract, adding checks for stale or wrong values, testing the failure cases on a test network and monitoring the feed after launch.

What is a blockchain oracle, and how do you implement one?

A blockchain oracle is a service that brings outside data onto a blockchain so smart contracts can use it, and implementing one means choosing a data source, connecting it to your contract and checking that the data stays correct. Blockchains cannot read the outside world on their own, so an oracle fills that gap.

The core idea is simple: a smart contract needs a price, a score, a delivery confirmation or a weather reading, and the oracle supplies it. The hard part is trust. If the oracle sends wrong data, the contract acts on wrong data, so the design must make bad data hard to slip through.

There are two broad approaches. You can use an existing oracle network, which is the common choice, or you can run your own oracle, which gives you control but adds work. Most teams start with an existing network and only build their own when they need data no network provides.

Which oracle approach fits your project?

The right oracle approach depends on what data you need, how often it changes and how much a wrong value would cost you. A lending app that reads a token price every few seconds has different needs from a shipping app that reads one delivery confirmation a day.

Approach Best for Control Work involved
Oracle network Prices, common data feeds Low Low
Single data provider One trusted source Medium Medium
Multiple providers High-value contracts Medium Medium
Own oracle node Custom or private data High High

For price feeds, an established oracle network is the usual answer, because it aggregates many sources and has been tested by other projects. For a custom data point, such as a delivery scan from your own warehouse system, your own oracle is often the only option.

Cost is part of the choice too. Oracle networks charge per request or per update, and the running cost grows with how often your contract reads data. See how much a blockchain oracle costs before you commit to a design that updates every block.

How do you implement an oracle, step by step?

You implement a blockchain oracle by defining the data your contract needs, picking a source, wiring the feed into the contract, adding checks and testing the whole path on a test network before launch.

  1. Define the data precisely. Write down exactly what the contract needs: which asset, which unit, which source and how fresh the value must be.
  2. Pick the source and the feed. Choose an oracle network or provider that already publishes that data, and confirm it covers your chain.
  3. Design the contract interface. Decide how the contract asks for data and what it does if the feed is stale, missing or looks wrong.
  4. Add safety checks. Set a maximum age for values, a sanity range and a fallback value or pause state for bad data.
  5. Connect and test on a test network. Deploy the contract, feed it real test data and check that updates, delays and failures behave as planned.
  6. Audit the oracle path. Review who can change the feed, what happens if a provider goes down and how the contract recovers.
  7. Launch with monitoring. Watch feed freshness, provider uptime and any rejected values from day one.

The contract side is usually the smaller part of the work. The larger part is deciding what counts as correct data and what the contract should do when the data is wrong, missing or late.

How do you keep oracle data trustworthy?

You keep oracle data trustworthy by using more than one source, checking values against each other and giving the contract a way to reject data that looks wrong. A single source is a single point of failure.

Common protections include taking the median of several providers, so one bad value does not move the result, and rejecting values that change by more than a set amount in one update. Time checks matter too: a price from an hour ago should not trigger a trade that assumes a live price.

Some teams add a delay or a dispute window for high-value actions, so a wrong value can be caught before money moves. Others use a circuit breaker that pauses the contract when the feed stops updating. These controls add complexity, but for contracts that move real value they are worth it.

For background on what an oracle is, see what a blockchain oracle is. For the cost side, the oracle cost guide covers what providers charge and what running your own node costs.

Example (hypothetical): a lending app adds a price feed

Example (hypothetical): a small lending app lets users borrow a stablecoin against a token deposit. The contract needs the token price to decide when a loan is under-collateralised, so the team adds an oracle price feed.

They choose an oracle network that publishes the token price every few minutes from several exchanges. The contract takes the median of the reported values, rejects any update older than 10 minutes and pauses liquidations if the feed stops. The team tests the whole path on a test network, including a simulated stale feed and a simulated price jump.

The numbers are small and checked: the feed updates 12 times an hour, so a 10-minute freshness limit means at most one missed update before the contract pauses. If the token price is $2.00 and a user deposits 500 tokens, the collateral is worth $1,000, and a loan of $600 leaves a $400 buffer before liquidation logic starts.

The launch goes out with monitoring on feed freshness and rejected values. In the first month the feed pauses twice for a few minutes each time, and both pauses are caught by the monitoring before any user is affected.

What are the common mistakes?

The most common oracle mistakes are trusting a single source, ignoring stale data and building a contract that has no safe state when the feed fails.

  • One source, no cross-check. A single provider can be wrong, delayed or compromised, and the contract will act on it anyway.
  • No freshness limit. A value from hours ago can look valid to a contract that never checks the timestamp.
  • No fallback. If the feed stops, the contract should pause or use a safe default, not keep running on old data.
  • Too-frequent updates. Reading data every block costs more and rarely improves decisions; match the update rate to the use case.
  • Skipping the failure test. Teams test the happy path and forget to test what happens when the oracle is down.

How do you choose an oracle provider?

You choose an oracle provider by checking that it covers your chain and your data, that it has several independent sources, and that its update rate and pricing fit your contract.

Ask three questions before you commit. First, does the provider already publish the exact data you need, or will you need a custom feed? Second, how many independent sources sit behind the feed, and what happens if one goes down? Third, what does it cost at your expected request rate, and does the cost grow faster than your usage?

Start on a test network with the provider's standard feed, and only build a custom feed when the standard one cannot supply your data. If your contract needs a custom data point, such as a delivery scan or a sensor reading, you will likely need your own oracle node, and smart contract development help is worth getting for the contract side.

Key takeaways

  • A blockchain oracle brings outside data, such as prices, onto a chain so contracts can use it
  • Most teams start with an existing oracle network and only build their own for custom data
  • Use several sources and take the median so one bad value cannot move the contract
  • Set a freshness limit and a safe paused state for when the feed stops
  • Test the failure cases, not just the happy path, before you launch

Frequently asked questions

What is a blockchain oracle?

A blockchain oracle is a service that supplies outside data to a smart contract, such as a token price, a delivery confirmation or a sports result. Blockchains cannot read the outside world on their own, so the oracle bridges that gap. The contract trusts the oracle, which is why the choice of source and checks matter so much.

Do I need to build my own oracle?

Usually not. Existing oracle networks already publish common data such as token prices, and using one is faster and cheaper than running your own node. You only need your own oracle when the data is custom or private, such as a reading from your own warehouse system or sensor network.

How much does it cost to run an oracle?

It depends on the provider and how often your contract reads data. Oracle networks charge per request or per update, so a feed that updates every block costs far more than one that updates hourly. See the oracle cost guide for the main pricing models and what running your own node involves.

How do I know the oracle data is correct?

Use more than one independent source and take the median, reject values that are stale or outside a sane range, and add a pause state for when the feed stops. No single check is enough on its own; the combination of sources, freshness limits and a circuit breaker is what makes the data trustworthy.

What happens if the oracle feed stops?

The contract should pause or fall back to a safe state, not keep running on old data. Set a maximum age for values and a circuit breaker that stops sensitive actions when the feed is stale. Test this failure case on a test network before launch, since it is the most commonly skipped test.

Which chains support oracles?

Most major chains have oracle networks available, including Ethereum and its layer-2 networks, as well as other smart contract chains. Check that your chosen provider publishes feeds on your specific chain before you start building, since coverage varies by chain and by data type.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a contract that needs real-world data? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Smart Contract Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call