How to programmatically bid on English auctions on OpenSea

by Dan Viau

Perhaps you’ve been bidding on English auctions on OpenSea manually and now you want to automate the process.  Luckily for you, OpenSea offers a set of tools that make this fairly painless. 

In this article, you’ll learn how to:
    • Set up your dev environment
    • Place a bid
    • Avoid common issues

Let’s start by taking a look at the key part of the example script:

Setting up your environment

First, we’ll need to set up an environment where we can run our bidding script.  You can roll your own, of course, but I recommend starting by cloning this example repository.  This article will assume you’re working from that repo, but you’ll likely find some useful information even if you’re not. 

Open up the directory in your text editor and set up your .env file. 

  • INFURA_KEY should be your project ID. 
  • MNEMONIC should be the MetaMask seed phrase of the account you want to bid from.
  • MY_ADDRESS should be that seed phrase’s “Account 1” address.
  • TARGET_CONTRACT_ADDRESS should be the the address of the NFT contract you want to bid on.
  • NETWORK should be “mainnet” unless you’re tinkering around on Rinkeby, in which case it should be “rinkeby”.
  • API_KEY is completely optional.  But if you’re planning on making lots of bids over a short period of time, feel free to request an API key here.

Now that your .env file is configured, navigate to the directory in a terminal window and source it by entering source .env on the command line.  This will make our script aware of the values we set up in the .env file when we run node bid.js later.

Next, enter nvm use 8.11.2 on the command line.  If that command doesn’t work, you might need to install node version 8.11.2 first.  Using the right version is imperative here! Using the wrong version will result in lots of issues.

Now, install the dependencies by running npm install.  You need to make sure you’re using 8.11.2 before installing the dependencies.  You can enter node --version to verify that you’re running the correct version.  If something goes wrong or if you realize that you ran npm_install using the wrong version of node, you should delete your node_modules folder before running `npm install again.

Once your .env file is configured and sourced and your dependencies are installed, you’re ready to configure your bidding script.

An example script

async function main() {
    const tokenId = '39'

    // Make a bid on an item. 
    console.log(`Bidding on https://opensea.io/assets/${TARGET_CONTRACT_ADDRESS}/${tokenId}`)
    try {
        const buyOrder = await seaport.createBuyOrder({
            asset: {
                tokenId,
                tokenAddress: TARGET_CONTRACT_ADDRESS
            },
            startAmount: .00002,
            accountAddress: MY_ADDRESS
        })
        console.log(`Successfully created a buy order! ${buyOrder.asset.openseaLink}\n`)
    } catch(e) {
        console.log('Failed:', e)
    }
    
}

If you set up your .env file properly, all you need to worry about is the main() function shown above.  Right now, const tokenId = '39' is hardcoded at the top of the main function. At a minimum, you’ll need to change the value to the ID of the item you want to bid on.  You’ll also want to change the hardcoded startAmount value to your desired bid amount. If you want to learn more about what’s going on in createBuyOrder, it’s well documented in our SDK.  And don’t forget to check the SDK’s README for tons of useful information.

Eventually, once you’ve tested this script in the default configuration, you’ll want to develop a better way of setting the token ID.  If you’re planning on leaving the script in roughly its current form, I recommend passing in the token ID, the address, and the bid amount as command line arguments.  See this article for more info on how to handle command line arguments in node.

Once you’ve configured the token ID and your bid amount, go to your terminal window and run node bid.js.  That runs the script.

Wait a few seconds, and you should see a “Bidding on…” message, with your custom values in the place of the variable names.  Wait a few more seconds, and you should see:

Already approved enough currency for trading
Order hashes match
Successfully created a buy order! https://opensea.io/assets/<TARGET_CONTRACT_ADDRESS>/<tokenId>

Navigate to the link that gets logged in the console to verify that your bid is showing up on the item.

That’s it, congrats! With this foundation, it should be a little easier to start doing all kinds of fun and exciting programmatic bidding on OpenSea English auctions.

Common gotchas

If you get “Failed: Error: API Error 404: Not found. Full message was ‘{“success”:false}'”, that means that OpenSea believes the asset you entered doesn’t exist.  Double check your address and your token ID. Run source .env again just to be safe.

If you run the script and it fails silently or complains of a nonexistent network, double check your Infura key.  It should be the project ID, not the project secret. You can also try nvm use 8.11.2 && rm -rf node_modules && npm i to make sure that you’re using an acceptable version of node.  Any 8.11.x version should work, but I always use 8.11.2.

What’s next?

For more info on programmatically finding assets you’re interested in, visit our SDK repository and our API reference docs.

Once you’ve got an array of assets you want to bid on, you could iterate over them and bid on each. This article only intends to provide a starting point. It’s up to you where to go from here!

If you have any questions or want to share something you built, ping me on Twitter.

Related Posts

Leave a Comment