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

# Set preference

> Set a recipient's subscription state for a specific target.

The `target` here can be used to set a project pereference (global-level) or a (resource-level) preference.

You can use this API to :

* Allow recipient to update their project preference on the "Notification's setting" screen.
* Allow recipient to subscribe to something. Example, "Subscribe a channel on YouTube" via `target` like `yt_channel:mr_beast:new_video` with `subscribed: true`.


## OpenAPI

````yaml PATCH /recipients/{recipient_id}/preferences
openapi: 3.0.3
info:
  title: Bodhveda Notifications API
  version: '1.0'
  description: API for sending notifications to recipients or broadcasting to all.
servers: []
security: []
paths:
  /recipients/{recipient_id}/preferences:
    patch:
      tags:
        - Preferences
      summary: Set recipient's subscription state for a target
      description: Set a recipient's subscription state for a specific target.
      operationId: setRecipientPreference
      parameters:
        - name: recipient_id
          in: path
          required: true
          schema:
            type: string
          description: The unique identifier of the recipient.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                target:
                  $ref: '#/components/schemas/Target'
                medium:
                  type: string
                  description: >-
                    The medium this preference applies to. In-app and email are
                    toggled independently for the same target. Defaults to
                    `in_app` when omitted.
                  enum:
                    - in_app
                    - email
                  default: in_app
                state:
                  type: object
                  properties:
                    enabled:
                      type: boolean
                      description: >-
                        Whether the recipient is subscribed to this target for
                        this medium.
                  required:
                    - enabled
              required:
                - target
                - state
      responses:
        '200':
          description: Updated subscription state for the target
          content:
            application/json:
              examples:
                Updated Target Subscription State:
                  summary: Updated Target Subscription State Response
                  value:
                    data:
                      target:
                        channel: posts
                        topic: post_id_123
                        event: new_comment
                        medium: email
                      state:
                        enabled: false
                        inherited: false
      security:
        - BearerAuthWithAPIKeyWithEitherScope: []
      x-codeSamples:
        - lang: curl
          label: cURL
          source: >-
            curl -X PATCH
            https://api.bodhveda.com/recipients/recipient_123/preferences \
              -H "Authorization: Bearer bv_xxxxxxxxx" \
              -H "Content-Type: application/json" \
              -d '{
                "target": {
                  "channel": "posts",
                  "topic": "post_id_123",
                  "event": "new_comment"
                },
                "medium": "email",
                "state": {
                  "enabled": false
                }
              }' 
        - lang: javascript
          label: JavaScript
          source: >-
            import { Bodhveda } from "@bodhveda/js";


            const bodhveda = new Bodhveda("bv_xxxxxxxxx");


            const pref = await
            bodhveda.recipients.preferences.set("recipient_123", {
                target: { channel: "posts", topic: "post_id_123", event: "new_comment" },
                medium: "email",
                state: { enabled: false },
            });
        - lang: go
          label: Go
          source: >-
            import (
                "context"

                bodhveda "github.com/MudgalLabs/bodhveda/sdk/go"
            )


            ctx := context.Background()

            client := bodhveda.NewClient("bv_xxxxxxxxx", nil)


            req := &bodhveda.SetPreferenceRequest{
                Target: bodhveda.Target{Channel: "posts", Topic: "post_id_123", Event: "new_comment"},
                Medium: bodhveda.MediumEmail,
            }

            req.State.Enabled = false

            pref, _ := client.Recipients.Preferences.Set(ctx, "recipient_123",
            req)
        - lang: jsx
          label: React
          source: >-
            import { useUpdatePreference } from "@bodhveda/react";


            // Requires a <BodhvedaProvider recipientID="recipient_123"> higher
            in the tree.

            function Toggle() {
                const { mutate } = useUpdatePreference();
                return (
                    <button
                        onClick={() =>
                            mutate({
                                target: { channel: "posts", topic: "post_id_123", event: "new_comment" },
                                medium: "email",
                                state: { enabled: false },
                            })
                        }
                    >
                        Unsubscribe
                    </button>
                );
            }
components:
  schemas:
    Target:
      type: object
      description: >-
        A **target** is the structure used to describe _who should get a
        notification_ and _how it should be categorized_.
      properties:
        channel:
          type: string
          description: Broad category for a notification (e.g., "posts").
        topic:
          type: string
          description: >-
            Subcategory inside the channel (e.g., "post_id_123"). 

            - `any` and `none` have special meaning. 

            - `any` means **all topics under this channel/event**. 

            - `none` means **only the base channel/event**. 

            - `any` is **not** a valid `topic` when sending a notification, but
            both are valid for preferences. 

             A preference with the target `posts:any:new_comment` will match a notification with target `posts:post_id_123:new_comment` but a preference with the target `posts:none:new_comment` will **only** match a notification with target `posts:none:new_comment`.
        event:
          type: string
          description: Event that triggered this notification (e.g., "new_comment").
      required:
        - channel
        - topic
        - event
  securitySchemes:
    BearerAuthWithAPIKeyWithEitherScope:
      type: http
      scheme: bearer
      bearerFormat: APIKey
      description: >-
        Bearer authentication header of the form `Bearer bv_xxxxxxxxx`, where
        `bv_xxxxxxxxx` is your **API Key** with `Full access` or `Recipient
        acess` scope.

````