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
  const OPENFORMAT_API_BASE_URL = "https://api.openformat.tech";

  // 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(
    "https://rpc-mumbai.maticvigil.com"
  );

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

  // Configure the API client for Openformat, setting the base URL and default headers
  const apiClient = axios.create({
    baseURL: OPENFORMAT_API_BASE_URL,
    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) => res.data)
    .catch((err) => console.error("Error fetching challenge:", err));

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

  // 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) => res.data)
    .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)
);