Manage API keys

In this guide, we will discuss adding and deleting API Keys to your MetaKeep App programmatically using the MetaKeep admin APIs. You can find end-to-end working scripts here.

Prerequisites

Please follow the previous guide to create and update MetaKeep apps programmatically using the MetaKeep admin APIs.

Manage API Keys

Add API Keys

MetaKeep API keys are a public/private ECC pair on the P-256 curve. The public key is used to identify the API key, and the private key, which is the API secret, is used to sign messages. To add a new API key to your app, first, generate a new key pair using the Web Crypto API.

const keyPair = await subtle.generateKey(
  {
    name: "ECDSA",
    namedCurve: "P-256",
  },
  true,
  ["sign", "verify"]
);

Then, prepare the request payload with the new API key and the signed message. The signed message is generated by signing the message "Hello" using the private key of the new key pair to prove ownership of the private key and encoded as base64.

{
  "appId": "appId",
  "apiKeys": {
    "addApiKeys": [
      {
        "name": "New-API-Key",
        "apiKey": "publicKey",
        "signedHelloMessage": "signedMessageB64"
      }
    ]
  }
}

Then, make a POST request to the /v2/app/update API to add the new API key to your app.

Delete API Keys

To delete API keys from your app, first use /v2/app/list API to get the app details with the API keys. Then, prepare the request payload with the API keys you want to delete.

{
  "appId": "appId",
  "apiKeys": {
    "deleteApiKeys": [
      {
        "apiKey": "api-key-to-delete"
      }
    ]
  }
}

Then, make a POST request to the /v2/app/update API to delete the API keys from your app.

Running the demo Scripts

You can find end-to-end working scripts here. To run the script, follow the steps below:

Step 1: Clone the MetaKeepCodeSamples repository and navigate to the admin-apis directory.

Step 2: Install all dependencies using npm install.

Step 3: Update ACCOUNT_KEY and ACCOUNT_SECRET in the .env file.

Step 4: Update APP_ID in the .env file with the appId of the app where you want to add or delete API keys.

Step 5: Run npm run addApiKey to add a new API key.

Step 6: Run npm run deleteApiKeys to delete API keys from the app.

You have now successfully added and deleted API keys to your app programmatically using your account key.