Javascript - ethers v5
import axios from "axios";
import { ethers } from "ethers";

// This asynchronous function generates an API key by authenticating with the Openformat API
// using an Ethereum account. It involves signing a challenge to prove ownership of the account.
async function generateAPIKey() {
  // Base URL for the Openformat API

  // Your Ethereum private key; must be associated with the account that registered the app on OPENFORMAT
  const ETH_PRIVATE_KEY = "";

  // Initialize a blockchain provider pointing to the Mumbai test network
  const blockchainProvider = new ethers.providers.JsonRpcProvider(

  // Create a wallet signer with the private key and provider to sign transactions and messages
  const wallet = new ethers.Wallet(

  // Configure the API client for Openformat, setting the base URL and default headers
  const apiClient = axios.create({
    headers: { "Content-Type": "application/json" },

  // Request a challenge from the Openformat API to sign, proving control of the Ethereum account
  const challengeResponse = await apiClient
    .post("/key/challenge", { public_address: wallet.address })
    .then((res) =>
    .catch((err) => console.error("Error fetching challenge:", err));

  // Sign the received challenge message using the wallet
  const signedMessage = await wallet.signMessage(

  // Verify the signed message with the Openformat API to complete authentication
  const verificationResponse = await apiClient
    .post("/key/verify", {
      signature: signedMessage,
      public_address: wallet.address,
    .then((res) =>
    .catch((err) =>
      console.error("Error verifying signed challenge:", err)

  // Output the newly generated API key
  console.log("API Key:", verificationResponse.api_key);

// Execute the function to generate the API Key, logging any errors encountered
generateAPIKey().catch((err) =>
  console.error("Error generating API key:", err)