proxyExecute

Executes API calls using encrypted API keys within a trusted execution environment (TEE).

Note: decryption can currently only be done in a client/browser environment. If you want to decrypt something server-side, please use the server-sdk. We will be updating the docs with instructions for this SDK soon!

Signature

async function proxyExecute(
  dataIdentifier: string,
  wallet: ethers.Wallet,
  request: {
    method: string,
    url: string,
    headers?: Record<string, string>,
    body?: any
  },
  config: {
    chain: string,
    apiUrl: string,
    expiration: string,
    permissionsRegistryContractAddress: string
  },
  debug?: boolean
): Promise<any>

Description

The proxyExecute function allows you to make API calls using encrypted API keys that were previously encrypted using encryptForProxy. The function executes the API call within a trusted execution environment (TEE), ensuring that the API key doesn’t leak to the end user.

Parameters

ParameterTypeRequiredDescription
dataIdentifierstringYesThe identifier of the encrypted API key (from encryptForProxy result).
walletethers.WalletYesThe wallet that has permission to use the encrypted API key.
requestobjectYesThe API request configuration object.
configobjectYesConfiguration object containing chain, API endpoints, and contract addresses.
debugbooleanNoWhen set to true, enables debug statements during the proxy execution. Default is false.

Request Object Properties

PropertyTypeRequiredDescription
methodstringYesThe HTTP method (e.g., ‘GET’, ‘POST’, ‘PUT’).
urlstringYesThe API endpoint URL.
headersRecord<string, string>NoAdditional HTTP headers for the request.
bodyanyNoThe request body (for POST, PUT, etc.).

Config Object Properties

Note: use init to automatically load the config.

PropertyTypeRequiredDescription
chainstringYesThe blockchain chain identifier.
apiUrlstringYesThe API endpoint for the proxy service.
expirationstringYesThe expiration time for the session.
permissionsRegistryContractAddressstringYesThe address of the permissions registry contract.

Returns

Promise<any> - A Promise that resolves to the API response data from the proxy service.

Examples

First, encrypt your API key using encryptForProxy:

import { init, proxyExecute } from "@keypo/typescript-sdk";
import { ethers } from "ethers";
 
// Prepare and encrypt the API key
const config = await keypo.init("https://api.keypo.io");
const apiKey = "sk-1234567890abcdef";
const { dataOut, metadataOut } = await keypo.preProcess(apiKey, 'openai-api-key');
const result = await keypo.encryptForProxy(
  dataOut,
  walletClient,
  metadataOut,
  config.encryptForProxyConfig,
  authorization
);

Then use proxyExecute to make API calls:

// Make an API call using the encrypted API key
const response = await keypo.proxyExecute(
  result.dataIdentifier,
  wallet,
  {
    method: "POST",
    url: "https://api.openai.com/v1/chat/completions",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "${apiKey}"  // API key will be replaced by the decrypted API key in TEE
    },
    body: {
      model: "gpt-3.5-turbo",
      messages: [{ role: "user", content: "Write one sentence about a cat" }]
    }
  },
  config.proxyExecuteConfig
);
 
console.log('API Response:', response);

Notes

  • The API key is never exposed outside the trusted execution environment (TEE).
  • The function automatically handles authentication using the encrypted API key.
  • The API response is limited to 100kb. If the response is larger, it will return a truncated response.
  • Only wallets with appropriate permissions can use the encrypted API key.
  • The function supports all standard HTTP methods and request configurations.
  • When debug is enabled, the function will log detailed information about the authentication process and request details.
  • The response is returned directly from the proxy service without modification.

See Also