February 2, 2024

How to Build Dapps on Optimism in 5 Steps – Optimism API Guide

Table of Contents

In today’s article, we’ll show you how to build dapps on Optimism with Moralis’ industry-leading Web3 API suite. With our premier development tools, you can seamlessly fetch and integrate on-chain data into your Optimism projects with only single lines of code. If you’re eager to get going, then here’s a little sneak peek of three endpoints you’ll likely find helpful when building Optimism dapps (decentralized applications):

  • getNativeBalance() – Get the native balance of any wallet: 
const response = await Moralis.EvmApi.balance.getNativeBalance({
  "chain": "0xa",
  "address": "0xCBA6a2397b322CF1389f6d1adc05F75F36B20116"
});
  • getWalletNFTs() – Query all NFTs owned by an address: 
const response = await Moralis.EvmApi.nft.getWalletNFTs({
  "chain": "0xa",
  "address": "0xCBA6a2397b322CF1389f6d1adc05F75F36B20116"
});
  • getTokenPrice() – Fetch the price of any token: 
const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0xa",
  "address": "0x4200000000000000000000000000000000000042"
});

The endpoints above are only three prominent examples, and if you’d like to explore the power of Moralis further, please join us in this guide on how to build dapps on Optimism! Also, if you want to follow along in this tutorial, don’t forget to sign up with Moralis. You can create an account for free, and you’ll gain instant access to our industry-leading Web3 APIs! 

Overview 

In today’s article, we’ll kick things off with an overview of building dapps on Optimism. From there, we’ll then jump straight into our tutorial, where we’ll show you how to build dapps on Optimism in five steps using Moralis industry-leading Optimism APIs

  • Step 1 – Create a Free Dev Account
  • Step 2 – Set Up Your Project
  • Step 3 – Choose Your Optimism API Endpoints
  • Step 4 – Write a Script Calling the Optimism API
  • Step 5 – Run the Code

Lastly, to top things off, we’ll also list some of the leading platforms on Optimism to give you some inspiration for what you can build in the future.

If this sounds exciting, join us below as we kick things off by diving straight into our overview of building dapps on Optimism! 

An Overview of Building Dapps on Optimism 

Optimism launched in 2021, and it’s a leading EVM-compatible ETH layer-2 blockchain. By utilizing optimistic roll-up technology, Optimism efficiently bundles hundreds of off-chain operations into a single Ethereum layer-1 transaction. This approach significantly enhances Ethereum’s scalability by facilitating quicker transactions at a low cost, providing a more efficient and cost-effective solution for users and developers alike! 

So, how do you build decentralized applications (dapps) on Optimism?

Anyone who wants to build top Optimism dapps needs access to an Optimism-compatible API. An API – short for ”application programming interface” – is a set of rules, protocols, and methods. Furthermore, these allow you to seamlessly interact with a blockchain network to integrate Web3 functionality and on-chain data into your projects. Without APIs, you would generally need to set up the underlying infrastructure for querying the various blockchain networks yourself, which is a tedious and time-consuming task. 

Anyone looking to bring their dapps to more than just one network in the future should also make sure to use cross-chain compatible APIs. This will make it super easy for you to expand your projects across the biggest chains, allowing you to reach a significantly broader user base. 

But where do you find the best Optimism API? 

Well, at the top of the list of premier Web3 API providers, you’ll find Moralis! 

Moralis’ Web3 APIs offer industry-leading response times and allow you to reduce the average time to market for all your blockchain projects by a staggering 83%. In addition, our APIs are cross-chain compatible, supporting networks like Ethereum, Polygon, Solana, and, of course, Optimism.

If you’d like to learn more about how you can build dapps on Optimism with Moralis, join us in the next section, as we’ll walk you through a comprehensive five-step tutorial! 

Build Dapps on Optimism in 5 Steps with Moralis’ API

In this section, we’re going to introduce you to Optimism dapp development with Moralis. In doing so, we’ll walk you through a brief five-step tutorial on how to build dapps on Optimism: 

  • Step 1 – Create a Free Dev Account
  • Step 2 – Set Up Your Project
  • Step 3 – Choose Your API Endpoints
  • Step 4 – Write a Script Calling the Optimism API
  • Step 5 – Run the Code

So, without further delay, let’s kick things off by creating a free Moralis dev account! 

Step 1 – Create a Free Dev Account 

If you don’t already have an account, the first thing you need to do is to click on the ”Start for Free” button at the top right of Moralis’ website:

Next, enter your full name, add an email address, and set up a password: 

Alternatively, you can also create an account using your current Google credentials: 

Setting up an account will take you to our onboarding process, where we initially encourage you to tell us a bit about yourself: 

From there, you’ll then get to choose a plan that suits your needs as a developer: 

That’s it; you now have a free Moralis dev account and are ready to proceed to the second step! 

Step 2 – Set Up Your Optimism Project

After creating your account, Moralis will automatically set up an initial project for you: 

The first thing you might want to do is rename your project under the ”Settings” tab: 

From there, you can then start exploring the various tabs in the menu to the left. For instance, on the ”Home” tab, you’ll find a list of our APIs:

Or, on the ”Playground” tab, you can seamlessly try some of our most well-used endpoints: 

However, it’s under the ”Settings” tab that you’ll find your API key, which you’re going to need in order to call the endpoints of our various APIs:

That covers the quick process of setting up your first Moralis project. In the next section, we’ll introduce you to our various APIs to help you choose the endpoints you need to build your dapps on Optimism! 

Step 3 – Choose Your Optimism API Endpoints 

In Moralis’ suite of Web3 development tools, you’ll find more than ten premier APIs, including the NFT API, Wallet API, Token API, and many others. With these tools, you can easily build everything from decentralized exchanges (DEXs) and NFT marketplaces to cryptocurrency wallets on Optimism without breaking a sweat! 

To give you an overview of what types of Optimism dapps you can build with our industry-leading tools, we’ll explore the following three Optimism APIs and their respective endpoints in further detail: 

  • NFT API
  • Wallet API
  • Token API

If you’d like to explore all our available interfaces, please check out the official Web3 API page. Now, let’s kick things off by diving into the ins and outs of Moralis’ premier NFT API! 

NFT API 

Moralis’ NFT API is the industry’s most comprehensive development tool for NFT data. The NFT API supports over three million NFT collections across ten+ EVM-compatible chains, including Ethereum, Polygon, and Optimism! 

With only single lines of code, you can use the NFT API to fetch enriched metadata, real-time transfers, on-chain price data, and much more for any NFT. Consequently, when working with this API, you can effortlessly build NFT dapps on Optimism. 

To showcase the power of the NFT API, here are three endpoints you’ll likely find helpful in your Optimism development endeavors: 

  • getWalletNFTs() – Get the NFT balance of any wallet: 
const response = await Moralis.EvmApi.nft.getWalletNFTs({
  "chain": "0xa",
  "address": "0xCBA6a2397b322CF1389f6d1adc05F75F36B20116"
});
  • getNFTMetadata() – Query the metadata of any NFT: 
const response = await Moralis.EvmApi.nft.getNFTMetadata({
  "chain": "0xa",
  "address": "0xB8Df6Cc3050cC02F967Db1eE48330bA23276A492",
  "tokenId": "1"
});
  • getNFTOwners() – Fetch the NFT owners for a given contract: 
const response = await Moralis.EvmApi.nft.getNFTOwners({
  "chain": "0xa",
  "address": "0xB8Df6Cc3050cC02F967Db1eE48330bA23276A492"
});

To explore this tool further, please check out our official NFT API page! 

Wallet API 

The Wallet API is the ultimate solution for wallet builders and anyone looking to integrate wallet functionality into their Optimism projects. This API supports over 500 million addresses across the most popular chains. This includes Ethereum, Polygon, BNB Smart Chain (BSC), Optimism, and others! 

With the Wallet API, you can seamlessly get the native balance, NFT balance, token transfers, profile data, and much more of any wallet address. As such, this industry-leading API gives you everything you need to build wallet dapps on Optimism. 

Here are three prominent endpoints to highlight the power of the Wallet API: 

  • getNativeBalance() – Fetch the native balance of any wallet: 
const response = await Moralis.EvmApi.balance.getNativeBalance({
  "chain": "0xa",
  "address": "0x2a82ae142b2e62cb7d10b55e323acb1cab663a26"
});
  • getWalletTokenBalance() – Query the token balances of any wallet: 
const response = await Moralis.EvmApi.token.getWalletTokenBalances({
  "chain": "0xa",
  "address": "0x2a82ae142b2e62cb7d10b55e323acb1cab663a26"
});
  • getWalletTransactions() – Get the native transaction history of any wallet: 
const response = await Moralis.EvmApi.transaction.getWalletTransactions({
  "chain": "0xa",
  "address": "0x2a82ae142b2e62cb7d10b55e323acb1cab663a26"
});

To learn more about this tool, please check out the official Wallet API page!

Token API 

Moralis’ Token API is the ultimate tool for fungible token data. The Token API supports every single token across ten+ EVM blockchains and counting. This includes everything from stablecoins like USDT to meme coins like TUX! 

With only a single API call, you can use the Token API to fetch token prices, transfers, balances, metadata, and more. Consequently, when working with the Token API, you can build everything from token explorers to DEXs with ease. 

To highlight the power of this tool, here are three prominent endpoints you’ll likely find useful when building Optimism dapps: 

  • getTokenMetadata() – Get the metadata for any fungible token: 
const response = await Moralis.EvmApi.token.getTokenMetadata({
  "chain": "0xa",
  "addresses": [
    "0x4200000000000000000000000000000000000042"
  ]
});
  • getTokenPrice() – Query the price of any token:
const response = await Moralis.EvmApi.token.getTokenPrice({
  "chain": "0xa",
  "address": "0x4200000000000000000000000000000000000042"
});
  • getTokenTransfers() – Fetch all token transactions of a given contract: 
const response = await Moralis.EvmApi.token.getTokenTransfers({
  "chain": "0xa",
  "address": "0x4200000000000000000000000000000000000042"
});

If you want to dive deeper into this interface, please visit the Token API page! 

Step 4 – Write a Script Calling the Optimism API 

In this section, we’ll show you how to call the various endpoints of Moralis’ Optimism API. To do so, we’ll write a short script for fetching the native balance of a wallet with the getNativeBalance() endpoint. However, this is simply an Optimism API example, and you can follow roughly the same procedure for any of our endpoints! 

Before continuing, make sure you have the following ready: 

  • Node.js v14+
  • NPM/Yarn

From here, set up a new folder in your IDE and initialize a new project by running the following command in a new terminal: 

npm init

Next, install the Moralis SDK with the following terminal command:

npm install moralis @moralisweb3/common-evm-utils

You then need to open your ”package.json” file and add ”type”: ”module” to the list: 

Next, create a new ”index.js” file and add the following code: 

import Moralis from 'moralis';

try {
  await Moralis.start({
    apiKey: "YOUR_API_KEY"
  });

  const response = await Moralis.EvmApi.balance.getNativeBalance({
    "chain": "0xa",
    "address": "0xCBA6a2397b322CF1389f6d1adc05F75F36B20116"
  });

  console.log(response.raw);
} catch (e) {
  console.error(e);
}

From here, you now need to configure the code slightly by replacing YOUR_API_KEY with your Moralis API key, along with modifying the chain and address parameters to fit your query.

That’s it; you’re now ready to run the script! 

Step 5 – Run the Code 

To execute the script, open a new terminal and run the command below in your project’s root folder: 

node index.js

In return, you’ll get a response that looks something like this: 

{
  "balance": "57881481011270850225"
}

That’s it! You now know how to fetch everything from wallet balances to token prices using Moralis’ Optimism API suite. Consequently, you’re now equipped with the skills to fetch on-chain data and can start to build your own dapps on Optimism! 

Project Inspiration – List of Optimism Dapps 

Now that you know how to build dapps on Optimism with Moralis’ API suite, let’s take this section to explore some already existing platforms for inspiration. More specifically, we’ll look at seven prominent projects on Optimism: 

List of Optimism Dapps
NameDescription
1. UniswapUniswap is an automated market maker (AMM) DEX for swapping cryptocurrency tokens.
2. StargateStargate is a liquidity transport protocol allowing users to bridge assets from other blockchains to Optimism securely.
3. AaveAave is a decentralized crypto platform that allows users to lend and borrow cryptocurrency. 
4. CurveCurve is an AMM-based DEX focusing on stablecoins like USDT, DAI, USDC, and more. 
5. Perpetual ProtocolPerpetual Protocol is a decentralized perpetual futures exchange. 
6. Trust WalletTrust Wallet is a leading self-custody cryptocurrency wallet that’s compatible with Optimism. 
7. MetaMaskMetaMask is one of the industry’s biggest self-custodial cryptocurrency wallets.

To learn more about projects on Optimism and other chains, feel free to check out Moralis’ ultimate dapp store: Web3 Wiki

Summary: How to Build Dapps on Optimism in 5 Steps – Optimism API Guide 

In today’s article, we showed you how to build dapps on Optimism in five steps using Moralis’ premier Web3 API suite: 

  • Step 1 – Create a Free Dev Account
  • Step 2 – Set Up Your Project
  • Step 3 – Choose Your Optimism API Endpoints
  • Step 4 – Write a Script Calling the Optimism API
  • Step 5 – Run the Code

If you have followed along this far, you now know how to fetch and integrate data from the Optimism blockchain into your projects! 

If you like this Optimism API tutorial, consider checking out more content here on the blog. For instance, read our guide on building cryptocurrency price trackers! Also, if you want to leverage Moralis’ Optimism-compatible APIs yourself, don’t forget to sign up with Moralis. You can create an account for free, and you’ll get instant access to our industry-leading suite of Web3 development tools! 

Market Data API
Build amazing trading and portfolio dapps with this Market Data API. Keep users engaged with up-to-date data!
Market Data API
Related Articles
May 20, 2024

How to Get the Full Supply of a Token 

May 29, 2024

Base Nodes – How to Run an RPC Node on Base

December 20, 2023

What is the Data Availability Layer in Crypto?

November 4, 2022

Web3 JS Tutorial – A Guide for Blockchain Developers

November 21, 2023

Gnosis Chain Deep Dive – Why Build on Gnosis Chain?

March 9, 2023

xNFT – What is an xNFT (Executable NFT)?

December 15, 2022

Ultimate Web3 Py Tutorial for Blockchain Developers

September 17, 2022

How to Set Up a Self-Hosted Web3 Server