> ## Documentation Index
> Fetch the complete documentation index at: https://doc.raliopay.com/llms.txt
> Use this file to discover all available pages before exploring further.

# KYC State Updated

> Webhook event fired when the KYC verification level changes

<Info>
  **Event Name:** `kyc_state_updated`
</Info>

<Note>
  This event is emitted when the overall KYC verification level changes, typically after a verification process is completed, downgraded, or expires.
</Note>

## Event Payload

The webhook payload contains the following structure:

<CodeGroup>
  ```json Example Payload (User) theme={null}
  {
    "eventId": "550e8400-e29b-41d4-a716-446655440000",
    "eventType": "kyc_state_updated",
    "timestamp": "2025-06-19T17:14:13.123Z",
    "data": {
      "kycId": 12345,
      "userId": "550e8400-e29b-41d4-a716-446655440001",
      "entityType": "user",
      "state": "REGULAR"
    }
  }
  ```

  ```json Example Payload (Related Party) theme={null}
  {
    "eventId": "550e8400-e29b-41d4-a716-446655440000",
    "eventType": "kyc_state_updated",
    "timestamp": "2025-06-19T17:14:13.123Z",
    "data": {
      "kycId": 12345,
      "relatedPartyId": "550e8400-e29b-41d4-a716-446655440003",
      "entityType": "related-party",
      "state": "REGULAR"
    }
  }
  ```
</CodeGroup>

## Field Description

<ParamField path="eventId" type="string" required>
  Unique event identifier (UUID)
</ParamField>

<ParamField path="eventType" type="string" required>
  Event type - always `kyc_state_updated` for this webhook
</ParamField>

<ParamField path="timestamp" type="string" required>
  Event timestamp in ISO 8601 format
</ParamField>

<ParamField path="data" type="object" required>
  Contains the event data
</ParamField>

<ParamField path="data.kycId" type="integer" required>
  Unique KYC record ID
</ParamField>

<ParamField path="data.entityType" type="string" required>
  Entity type associated with the KYC record. Possible values: `user`, `related-party`
</ParamField>

<ParamField path="data.userId" type="string">
  Unique user ID (UUID)
</ParamField>

<ParamField path="data.relatedPartyId" type="string">
  Unique related party ID (UUID)
</ParamField>

<ParamField path="data.state" type="string" required>
  Current KYC level. Common values include `REGULAR` and `LIGHT`
</ParamField>

## When This Event Is Sent

This webhook is sent when the KYC record changes its overall verification level.

Typical scenarios include:

* A verification flow upgrades the entity to `REGULAR`
* A review or business rule downgrades the entity to `LIGHT`
* A document expiration process updates the KYC level

## Expected Responses

<CardGroup cols={3}>
  <Card title="Success Response" icon="circle-check" color="#16a34a">
    **HTTP 200 OK**

    The webhook was processed successfully.
  </Card>

  <Card title="Client Error" icon="triangle-exclamation" color="#ea580c">
    **HTTP 4xx Status**

    Client-side error. Will not be retried.
  </Card>

  <Card title="Server Error" icon="circle-xmark" color="#dc2626">
    **HTTP 5xx Status**

    Server-side error. Will be retried with exponential backoff.
  </Card>
</CardGroup>

## Implementation Example

<CodeGroup>
  ```javascript Node.js theme={null}
  app.post('/webhooks/ralio', async (req, res) => {
    const { eventType, data } = req.body;

    if (eventType === 'kyc_state_updated') {
      const { kycId, entityType, userId, relatedPartyId, state } = data;
      const subjectId = userId ?? relatedPartyId;

      console.log(
        `KYC ${kycId} for ${entityType} ${subjectId} changed to ${state}`
      );

      if (entityType === 'user' && userId) {
        await updateUserKycLevel(userId, state);
      }

      if (entityType === 'related-party' && relatedPartyId) {
        await updateRelatedPartyKycLevel(relatedPartyId, state);
      }

      res.status(200).json({ received: true });
    }
  });
  ```

  ```python Python theme={null}
  @app.route('/webhooks/ralio', methods=['POST'])
  def handle_webhook():
      payload = request.get_json()

      if payload['eventType'] == 'kyc_state_updated':
          data = payload['data']
          kyc_id = data['kycId']
          entity_type = data['entityType']
          user_id = data.get('userId')
          related_party_id = data.get('relatedPartyId')
          state = data['state']
          subject_id = user_id or related_party_id

          print(f"KYC {kyc_id} for {entity_type} {subject_id} changed to {state}")

          if entity_type == 'user' and user_id:
              update_user_kyc_level(user_id, state)

          if entity_type == 'related-party' and related_party_id:
              update_related_party_kyc_level(related_party_id, state)

          return jsonify({'received': True}), 200
  ```

  ```go Go theme={null}
  func handleWebhook(w http.ResponseWriter, r *http.Request) {
      var payload struct {
          EventType string `json:"eventType"`
          Data  struct {
              KycID          int    `json:"kycId"`
              EntityType     string `json:"entityType"`
              UserID         string `json:"userId"`
              RelatedPartyID string `json:"relatedPartyId"`
              State          string `json:"state"`
          } `json:"data"`
      }

      if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
          http.Error(w, "Invalid JSON", http.StatusBadRequest)
          return
      }

      if payload.EventType == "kyc_state_updated" {
          subjectID := payload.Data.UserID
          if subjectID == "" {
              subjectID = payload.Data.RelatedPartyID
          }

          log.Printf(
              "KYC %d for %s %s changed to %s",
              payload.Data.KycID,
              payload.Data.EntityType,
              subjectID,
              payload.Data.State,
          )

          if payload.Data.EntityType == "user" && payload.Data.UserID != "" {
              if err := updateUserKycLevel(payload.Data.UserID, payload.Data.State); err != nil {
                  http.Error(w, "Internal error", http.StatusInternalServerError)
                  return
              }
          }

          if payload.Data.EntityType == "related-party" && payload.Data.RelatedPartyID != "" {
              if err := updateRelatedPartyKycLevel(payload.Data.RelatedPartyID, payload.Data.State); err != nil {
                  http.Error(w, "Internal error", http.StatusInternalServerError)
                  return
              }
          }

          w.Header().Set("Content-Type", "application/json")
          w.WriteHeader(http.StatusOK)
          json.NewEncoder(w).Encode(map[string]bool{"received": true})
      }
  }
  ```
</CodeGroup>
