Skip to main content
POST https://swarms.world/api/product/claimfees Claims accumulated fees for a token on Solana. You provide the token mint (contract address) and your wallet’s private key; the endpoint builds the claim transaction, signs it with your key, submits it on-chain, and returns the transaction signature and how much SOL was claimed. The private key is used only in memory to sign the transaction and is never stored or logged.

Request

Headers

NameTypeRequiredDescription
Content-TypestringYesMust be application/json.

Body Parameters (JSON)

ParameterTypeRequiredDescription
castringYesToken mint / contract address of the coin (Solana address). Must be 32–44 characters.
privateKeystringYesBase58-encoded wallet private key. Used only to sign the claim transaction; must be the fee-owner wallet.

Response

Success (HTTP 200)

FieldTypeDescription
successbooleanAlways true on success.
signaturestring | nullSolana transaction signature from the claim. May be null if the upstream response did not include a signature.
amountClaimedSolnumber | nullSOL amount that was claimed in this request (pre-claim unclaimed amount). null if fee info could not be fetched.
feesobject | nullFee breakdown (same as entity Creator Fees). null if fee info could not be fetched.
fees.unclaimedSolnumberUnclaimed SOL before this claim (same as amountClaimedSol on success).
fees.claimedSolnumberTotal SOL already claimed (historical).
fees.totalSolnumberTotal fees earned (unclaimed + claimed).
Example success response:
{
  "success": true,
  "signature": "5V7x...signature...",
  "amountClaimedSol": 0.42,
  "fees": {
    "unclaimedSol": 0.42,
    "claimedSol": 1.08,
    "totalSol": 1.5
  }
}
If fee info could not be fetched, amountClaimedSol and fees will be null; signature is still returned on success.

HTTP Status Codes

CodeMeaning
200Success; fees claimed and transaction submitted.
400Bad request: missing ca or privateKey, invalid token mint format, or invalid base58 private key.
405Method not allowed; only POST is accepted.
500Internal server error, or claim transaction failed.

Example Request

curl -X POST https://swarms.world/api/product/claimfees \
  -H "Content-Type: application/json" \
  -d '{
    "ca": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
    "privateKey": "YOUR_BASE58_PRIVATE_KEY"
  }'

Error Responses

Error response body (4xx / 5xx)

FieldTypeDescription
errorstringShort error message describing the failure.

Example error responses

Missing parameters (400):
{
  "error": "ca (token mint) and privateKey are required in request body"
}
Invalid token mint (400):
{
  "error": "Invalid ca (token mint) format"
}
Invalid private key (400):
{
  "error": "Invalid privateKey: could not decode base58 secret key"
}
Claim / submit failed (500): Returned when the claim transaction fails (e.g. no fees to claim, network error).
{
  "error": "Claim transaction failed"
}

Advanced Examples

These examples include additional features like retry logic, async support, context/timeout handling, and custom error types.
import { setTimeout } from "timers/promises";

interface ClaimFeesResponse {
  success: boolean;
  signature: string | null;
  amountClaimedSol: number | null;
  fees: {
    unclaimedSol: number;
    claimedSol: number;
    totalSol: number;
  } | null;
}

class ClaimFeesClient {
  private baseUrl = "https://swarms.world";
  private maxRetries: number;
  private retryDelayMs: number;

  constructor(options?: { maxRetries?: number; retryDelayMs?: number }) {
    this.maxRetries = options?.maxRetries ?? 3;
    this.retryDelayMs = options?.retryDelayMs ?? 1000;
  }

  async claimFees(
    ca: string,
    privateKey: string
  ): Promise<ClaimFeesResponse> {
    let lastError: Error | null = null;

    for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
      try {
        const response = await fetch(
          `${this.baseUrl}/api/product/claimfees`,
          {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ ca, privateKey }),
          }
        );

        const data = await response.json();

        if (!response.ok) {
          // Don't retry client errors (4xx)
          if (response.status >= 400 && response.status < 500) {
            throw new Error(data.error || `Client error: ${response.status}`);
          }
          throw new Error(data.error || `Server error: ${response.status}`);
        }

        return data as ClaimFeesResponse;
      } catch (error) {
        lastError = error as Error;
        console.warn(`Attempt ${attempt} failed:`, lastError.message);

        if (attempt < this.maxRetries) {
          const delay = this.retryDelayMs * Math.pow(2, attempt - 1);
          console.log(`Retrying in ${delay}ms...`);
          await setTimeout(delay);
        }
      }
    }

    throw lastError ?? new Error("Failed to claim fees");
  }
}

// Usage
async function main() {
  const client = new ClaimFeesClient({ maxRetries: 3, retryDelayMs: 1000 });

  try {
    const result = await client.claimFees(
      "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
      "YOUR_BASE58_PRIVATE_KEY"
    );

    console.log("Claim successful!");
    console.log("Transaction signature:", result.signature);

    if (result.amountClaimedSol !== null) {
      console.log(`Claimed ${result.amountClaimedSol} SOL`);
    }
  } catch (error) {
    console.error("Failed to claim fees:", error);
    process.exit(1);
  }
}

main();

Security Notes

Private Key Security: The privateKey is used only in memory to sign the claim transaction and is not stored or logged. However, sending a private key in any API request is inherently risky.
  • Use HTTPS: Always use HTTPS in production to encrypt the private key in transit.
  • Consider Client-Side Signing: For production applications where security is paramount, consider implementing client-side signing using a wallet adapter. This avoids sending the private key to the server entirely.
  • Key Rotation: If you suspect your private key has been compromised, immediately transfer funds to a new wallet.
  • Environment Variables: Never hardcode private keys in your source code. Use environment variables or secure secret management systems.

See Also