> For the complete documentation index, see [llms.txt](https://docs.yield.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yield.fi/earn-with-yieldfi/integration-sdk/authentication/consent.md).

# Consent

The YieldFi SDK provides comprehensive APIs for managing user consent records. Consent records are immutable for audit compliance - once recorded, they cannot be deleted or revoked.

### Consent Types

The SDK supports the following consent types:

* `TERMS_OF_SERVICE` - User acceptance of terms of service (required)
* `PRIVACY_POLICY` - User acceptance of privacy policy (required)
* `MARKETING` - User consent for marketing communications (optional)
* `ANALYTICS` - User consent for analytics tracking (optional)
* `COOKIES` - User consent for cookie usage (optional)

### Recording Consent

Record user consent when they accept terms, policies, or opt-in to features:

```typescript
import { YieldFiSDK, ConsentType } from "yieldfi-sdk";

const sdk = await YieldFiSDK.create({
  gatewayUrl: "https://gw.yield.fi",
});

const accessToken = localStorage.getItem("accessToken");

// Record terms of service acceptance
const consent = await sdk.auth.recordConsent(accessToken, {
  consentType: ConsentType.TERMS_OF_SERVICE,
  version: "1.0",
  granted: true,
  metadata: {
    documentHash: "0xabc123...",
    sourceUrl: "https://yield.fi/terms",
  },
});

console.log(`Consent recorded: ${consent.consent.id}`);
```

### Getting Consent Records

#### Get Specific Consent

Get a specific consent record by type and optional version:

```typescript
// Get latest consent for a type
const termsConsent = await sdk.auth.getConsent(
  accessToken,
  ConsentType.TERMS_OF_SERVICE
);

// Get specific version
const termsConsentV1 = await sdk.auth.getConsent(
  accessToken,
  ConsentType.TERMS_OF_SERVICE,
  "1.0"
);
```

#### Get All User Consents

Get the complete audit trail of all consent records:

```typescript
const allConsents = await sdk.auth.getUserConsents(accessToken);

console.log(`User has ${allConsents.count} consent records`);

allConsents.data.forEach((consent) => {
  console.log(`${consent.consentType} v${consent.version}: ${consent.granted ? "Granted" : "Denied"}`);
  console.log(`Recorded at: ${consent.recordedAt}`);
});
```

#### Get Consent Status Summary

Get the latest status for each consent type:

```typescript
const statuses = await sdk.auth.getConsentStatuses(accessToken);

// Check if user has consented to marketing
const marketingStatus = statuses.data.find(
  (s) => s.consentType === ConsentType.MARKETING
);

if (marketingStatus?.granted) {
  // User has consented, can send marketing emails
  sendMarketingEmail();
} else {
  // Show consent banner
  showConsentBanner();
}
```

### Complete Example: Consent Flow

Here's a complete example of implementing a consent flow:

```typescript
import { YieldFiSDK, ConsentType } from "yieldfi-sdk";
import { useState, useEffect } from "react";

function ConsentManager() {
  const [consentStatuses, setConsentStatuses] = useState<any>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    loadConsentStatuses();
  }, []);

  const loadConsentStatuses = async () => {
    try {
      const sdk = await YieldFiSDK.create({
        gatewayUrl: "https://gw.yield.fi",
      });

      const accessToken = localStorage.getItem("accessToken");
      const statuses = await sdk.auth.getConsentStatuses(accessToken);
      setConsentStatuses(statuses);
    } catch (error) {
      console.error("Failed to load consent statuses:", error);
    } finally {
      setLoading(false);
    }
  };

  const recordConsent = async (
    consentType: ConsentType,
    granted: boolean
  ) => {
    try {
      const sdk = await YieldFiSDK.create({
        gatewayUrl: "https://gw.yield.fi",
      });

      const accessToken = localStorage.getItem("accessToken");

      await sdk.auth.recordConsent(accessToken, {
        consentType,
        version: "1.0",
        granted,
        metadata: {
          sourceUrl: window.location.href,
        },
      });

      // Reload consent statuses
      await loadConsentStatuses();
    } catch (error) {
      console.error("Failed to record consent:", error);
    }
  };

  if (loading) {
    return <div>Loading consent statuses...</div>;
  }

  const termsStatus = consentStatuses?.data.find(
    (s: any) => s.consentType === ConsentType.TERMS_OF_SERVICE
  );
  const marketingStatus = consentStatuses?.data.find(
    (s: any) => s.consentType === ConsentType.MARKETING
  );

  return (
    <div>
      <h2>Consent Management</h2>

      {/* Terms of Service */}
      {!termsStatus?.granted && (
        <div>
          <p>Please accept our Terms of Service</p>
          <button onClick={() => recordConsent(ConsentType.TERMS_OF_SERVICE, true)}>
            Accept Terms
          </button>
        </div>
      )}

      {/* Marketing Consent */}
      {!marketingStatus && (
        <div>
          <p>Would you like to receive marketing communications?</p>
          <button onClick={() => recordConsent(ConsentType.MARKETING, true)}>
            Yes, I consent
          </button>
          <button onClick={() => recordConsent(ConsentType.MARKETING, false)}>
            No, thanks
          </button>
        </div>
      )}

      {/* Show current statuses */}
      <div>
        <h3>Current Consent Status</h3>
        {consentStatuses?.data.map((status: any) => (
          <div key={status.consentType}>
            <strong>{status.consentType}:</strong>{" "}
            {status.granted ? "✅ Granted" : "❌ Denied"} (v{status.version})
          </div>
        ))}
      </div>
    </div>
  );
}
```

### Important Notes

1. **Immutability**: Consent records cannot be deleted or revoked once created. This ensures audit compliance.
2. **Versioning**: Each consent type can have multiple versions (e.g., Terms v1.0, v2.0). Always record consent for the current version.
3. **Audit Trail**: All consent records include:
   * Timestamp
   * IP address
   * User agent
   * Metadata (document hash, source URL, etc.)
4. **Authentication Required**: All consent APIs require a valid access token.
5. **Required Consents**: Terms of Service and Privacy Policy are typically required before users can use the platform.

### Next Steps

* API Reference - Explore all available APIs
* Examples - See complete examples
