> ## Documentation Index
> Fetch the complete documentation index at: https://moengage-partner-guide.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Fetch Experience

> This API receives data on active personalization experiences. You can fetch data for one or more server-side experiences by using the **experience_key** field. MoEngage will evaluate targeting rules and in-session attributes automatically and return the correct variation for the user. Typically, you would make this call as part of your larger page and content rendering pipeline.




## OpenAPI

````yaml /api/personalize-experience/personalize-experience.yaml post /experiences/fetch
openapi: 3.0.3
info:
  title: Personalize APIs
  description: >
    Personalize APIs constitute a set of endpoints that can be accessed from
    your platform's code. These APIs offer personalization capabilities and must
    be invoked from your codebase for MoEngage to provide relevant data. After
    receiving the data, you can use it within your platform's code to
    personalize the content for your users.
  version: 1.0.0
servers:
  - url: https://sdk-{dc}.moengage.com/v1
    description: MoEngage API Server
    variables:
      dc:
        default: '01'
        description: >
          The 'X' in the API Endpoint URL refers to the MoEngage Data Center
          (DC). MoEngage hosts each customer in a different DC. You can find
          your DC number (value of X) and replace the value of 'X' in the URL by
          referring to the DC and API endpoint mapping
          [here](https://help.moengage.com/hc/en-us/articles/360057030512-Data-Centers-in-MoEngage).
security:
  - basicAuth: []
tags:
  - name: Experiences
    description: API endpoints for fetching experiences and metadata.
  - name: Events
    description: API endpoints for reporting impressions and clicks.
paths:
  /experiences/fetch:
    post:
      tags:
        - Experiences
      summary: Fetch Experience
      description: >
        This API receives data on active personalization experiences. You can
        fetch data for one or more server-side experiences by using the
        **experience_key** field. MoEngage will evaluate targeting rules and
        in-session attributes automatically and return the correct variation for
        the user. Typically, you would make this call as part of your larger
        page and content rendering pipeline.
      operationId: fetchExperience
      parameters:
        - name: MOE-APPKEY
          in: header
          required: true
          description: >
            This is the Workspace ID of your MoEngage account that must be
            passed with the request. You can find it in the MoEngage dashboard
            at **Settings** > **Account** > **APIs** > **Workspace ID (earlier
            app id)**.
          schema:
            type: string
            example: Workspace ID
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                identifiers:
                  type: object
                  properties:
                    customer_id:
                      type: string
                      description: >
                        Your brand provides this field, which should be pasted
                        in the request. Generally, this is the phone number,
                        email ID, or any other unique ID used to uniquely
                        identify the user in MoEngage.


                        This is essential for fetching experiences that are
                        configured for audiences segmented based on user
                        attributes/user behavior/user affinity/custom segments.


                        If the the **customer_id** is not part of the request
                        **OR** is inaccurate **OR** is not present in MoEngage,
                        **user_identifiers** to uniquely identify the user.


                        It is mandatory to have [Identity
                        Resolution](https://help.moengage.com/hc/en-us/articles/24050999467284-Unified-Identity-Identity-Resolution)
                        implemented for your workspace to use *user_identifiers*
                        as a fallback to **customer_id** for identifying the
                        user.
                    user_identifiers:
                      type: string
                      description: >
                        If you have [Identity
                        Resolution](https://help.moengage.com/hc/en-us/articles/24050999467284-Unified-Identity-Identity-Resolution)
                        enabled for your account, you can use different
                        attributes such as customer ID OR email ID OR mobile
                        number to uniquely identify a user.


                        Apart from email id or phone number, you can use any
                        custom user attribute that is being used as part of the
                        Identity Resolution.


                        If the **user_identifiers** is not part of the request
                        **OR** the **user_identifier** is not present in
                        MoEngage, the API will return experiences configured for
                        All users.
                experience_key:
                  type: array
                  items:
                    type: string
                  description: >
                    This field uniquely identifies each server-side experience
                    created using MoEngage Personalize. You can pass multiple
                    values in a single request and receive the personalized
                    content defined for each experience in the response.


                    To fetch the payload for a single experience:

                    `experience_key: ["experience-1"]`


                    To fetch payload for multiple experiences in a single call:

                    `experience_key: ["experience-1", "experience-2"]`


                    If no experience_key is specified, API will return:

                    * all experiences for the specified customer_id **OR**

                    * all experiences configured for **all users** if
                    customer_id is not part of the request
                Custom_Attribute:
                  type: string
                  description: >
                    This field is must contain the same value that has been used
                    to identify users using the In-session attribute *Query
                    Parameter*.


                    For example, if you want to target users who have opened the
                    app after clicking on a link in an e-mail campaign and you
                    have specified the parameter name as *utm_medium*,

                    `utm_medium:"email"`
                DAY_OF_THE_WEEK:
                  type: string
                  description: >
                    This field must contain the day of the week for evaluating
                    IN-session attribute-based experiences. Example:

                    `DAY_OF_THE_WEEK:"Sunday"`
                TIME_OF_THE_DAY:
                  type: string
                  description: >
                    This field must contain the time of the day for evaluating
                    IN-session attribute-based experiences.

                    Example: For a time interval between 7 PM - 8 PM, the input
                    for this field should be

                    `TIME_OF_THE_DAY: "00"`
                USER_IP_ADDRESS:
                  type: string
                  description: >-
                    This field must contain the user’s IP address to fetch
                    experiences for audiences segmented basis  geo-location.
                USER_AGENT:
                  type: string
                  description: >
                    This field must contain the USER-AGENT HTTP header.

                    This is useful to delivering experiences personalized based
                    on in-session attributes like Device Type.
      responses:
        '200':
          description: |
            **Success**
            This response is returned when the request is submitted to MoEngage.
          content:
            application/json:
              schema:
                type: object
                properties:
                  experiences:
                    type: object
                    additionalProperties:
                      type: object
                      properties:
                        payload:
                          type: object
                          description: >-
                            This field contains a list of all the key-value
                            pairs defined as part of the experience
                          additionalProperties:
                            type: object
                            properties:
                              value:
                                type: string
                                description: >-
                                  This field contains the value of the keys
                                  defined in the payload
                              data_type:
                                type: string
                                description: >-
                                  This field specifies the data type of the
                                  value of each key
                        experience_context:
                          type: object
                          description: >-
                            This field returns a JSON object that is to be used
                            for reporting pageviews/impressions and/or tracking
                            clicks within the personalized experience.
              examples:
                JSON:
                  value:
                    experiences:
                      new-serverside-experience:
                        payload:
                          Title:
                            value: 'Presenting MoEngage Server side personalization! '
                            data_type: string
                          Image:
                            value: >-
                              https://cdn.pixabay.com/photo/2021/02/24/20/53/abstract-6047465_960_720.jpg
                            data_type: string
                        experience_context:
                          cid: 65eae5738ea5032b0ef60138_F_T_WP_AB_2_P_0_AU_42D
                          experience: Server Side Experience
                          moe_locale_id: <id>
                          moe_variation_id: <id>
                          audience_name: <audience name>
                          audience_id: <id>
                          type: Web Personalization
        '400':
          description: |
            **Bad Request**
            This response is returned in case the payload is empty or invalid.
        '401':
          description: >
            **Authorization Failed**

            This response is returned when the authorization fails due to
            incorrect values for the APP KEY/ HTTP Auth Header.
        '500':
          description: >
            **Internal Server error**

            This response is returned when the system runs into an unexpected
            error.
      x-codeSamples:
        - lang: cURL
          label: cURL
          source: >
            curl --location 'https://sdk-0X.moengage.com/v1/experiences/fetch'

            --header 'Accept: */*'

            --header 'Content-Type: application/json'

            --header 'Authorization: Basic
            XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw=='

            --header 'MOE-APPKEY: <Your Workspace ID>'

            --data '{

            "identifiers": {

            "customer_id": "Unique identifier for the user mapped with ID
            field",

            "user_identifiers": {

            "u_em": "User Email",

            "u_mb": "Mobile Number"

            }
             },
            "experience_key": ["<experience-key>"],

            "Custom_attribute":"Example: utm_medium: email",

            "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday,
            Tuesday etc.",

            "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",

            "USER_IP_ADDRESS": "IP address for location based experiences",

            "USER_AGENT": "Copy the USER-AGENT http header from the request"

            }'
        - lang: JavaScript
          label: Node.js
          source: >
            import fetch from 'node-fetch';


            var result = await
            fetch('https://sdk-0X.moengage.com/v1/experiences/fetch', {

            method: 'POST',

            headers: {
                'Authorization': 'Basic XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw==',
                'Content-Type': 'application/json',
                'MOE-APPKEY': '<Your Workspace ID>' },
            body: JSON.stringify({
                "identifiers": {
                    "customer_id": "Unique identifier for the user mapped with ID field",
                    "user_identifiers": {
            "u_em": "User Email",

            "u_mb": "Mobile Number"
                    }
                },
                "experience_key": ["<experience-key>"],
                "Custom_attribute":"Example: utm_medium: email",
                "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday, Tuesday etc.",
                "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
                "USER_IP_ADDRESS": "IP address for location based experiences",
                "USER_AGENT": "Copy the USER-AGENT http header from the request"
            })

            });

            var data = await result.json()

            console.log(data)
        - lang: Go
          label: Go
          source: >
            package main
                 
            import (

            "fmt"

            "io/ioutil"

            "log"

            "net/http"

            "strings"

            )


            func main() {

            client := &http.Client{}

            var data = strings.NewReader(`{
                "identifiers": {
                    "customer_id": "Unique identifier for the user mapped with ID field",
                    "user_identifiers": {
            "u_em": "User Email",

            "u_mb": "Mobile Number"
                    }
                },
                "experience_key": ["<experience-key>"],
                "Custom_attribute":"Example: utm_medium: email",
                "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday, Tuesday etc.",
                "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
                "USER_IP_ADDRESS": "IP address for location based experiences",
                "USER_AGENT": "Copy the USER-AGENT http header from the request"
            }`)

            req, err := http.NewRequest("POST",
            "https://sdk-0X.moengage.com/v1/experiences/fetch", data)

            if err != nil {
                log.Fatal(err)
            }

            req.Header.Set("Authorization", "Basic
            XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw==")

            req.Header.Set("Content-Type", "application/json")

            req.Header.Set("MOE-APPKEY", "<Your Workspace ID>")

            resp, err := client.Do(req)

            if err != nil {
                log.Fatal(err)
            }

            defer resp.Body.Close()

            bodyText, err := ioutil.ReadAll(resp.Body)

            if err != nil {
                log.Fatal(err)
            }

            fmt.Printf("%s", bodyText)

            }
        - lang: PHP
          label: PHP
          source: >
            <?php

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL,
            'https://sdk-0X.moengage.com/v1/experiences/fetch');

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

            curl_setopt($ch, CURLOPT_HTTPHEADER, [

            'Authorization' = 'Basic
            XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw==',

            'Content-Type' = 'application/json',

            'MOE-APPKEY' = '<Your Workspace ID>',

            ]);

            curl_setopt($ch, CURLOPT_POSTFIELDS, '{"identifiers":
            {"customer_id": "Unique identifier for the user mapped with ID
            field","user_identifiers": {"u_em": "User Email","u_mb": "Mobile
            Number"}}, "experience_key": ["<experience-key>"],  
            "Custom_attribute":"Example: utm_medium: email", "DAY_OF_THE_WEEK":
            "Day of the week as a string in format: Monday, Tuesday etc.",
            "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
            "USER_IP_ADDRESS": "IP address for location based experiences",
            "USER_AGENT": "Copy the USER-AGENT http header from the request"}');

            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);


            $response = curl_exec($ch);


            curl_close($ch);   
        - lang: Python
          label: Python
          source: |
            import requests
                 
               url = 'https://sdk-0X.moengage.com/v1/experiences/fetch'
               headers = {
                   'Authorization': 'Basic XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw==',
                   'MOE-APPKEY', '<Your Workspace ID>'
               }
               data = {
                   "identifiers": {
              "customer_id": "Unique identifier for the user mapped with ID field",
              "user_identifiers": {
                  "u_em": "User Email",
                  "u_mb": "Mobile Number"
              }
                   },
                   "experience_key": ["<experience-key>"],
                   "Custom_attribute":"Example: utm_medium: email",
                   "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday, Tuesday etc.",
                   "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
                   "USER_IP_ADDRESS": "IP address for location based experiences",
                   "USER_AGENT": "Copy the USER-AGENT http header from the request"
               }
               
               response = requests.post(url, headers=headers, json=data)
               print(response.json())    
        - lang: Ruby
          label: Ruby
          source: >
            require 'net/http'

            require 'json'


            uri = URI('https://sdk-0X.moengage.com/v1/experiences/fetch')

            req = Net::HTTP::Post.new(uri)

            req.content_type = 'application/json'

            req['Authorization'] = 'Basic
            XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw=='

            req['MOE-APPKEY'] = '<Your Workspace ID>'

            # The object won't be serialized exactly like this

            # req.body =
            "{"alert_id":null,"customer_id":"CUSTOMER_ID","transaction_id":"YOUR_TRANSACTION_ID_VAL_HERE","payloads":{"SMS":{"recipient":""}}}"

            req.body = {
                "identifiers": {
                    "customer_id": "Unique identifier for the user mapped with ID field",
                    "user_identifiers": {
            "u_em": "User Email",

            "u_mb": "Mobile Number"
                    }
                },
                "experience_key": ["<experience-key>"],
                "Custom_attribute":"Example: utm_medium: email",
                "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday, Tuesday etc.",
                "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
                "USER_IP_ADDRESS": "IP address for location based experiences",
                "USER_AGENT": "Copy the USER-AGENT http header from the request"
            }.to_json


            req_options = {

            use_ssl: uri.scheme == "https"

            }

            res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|

            http.request(req)

            end
        - lang: Java
          label: Java
          source: >
            import java.io.IOException;

            import java.io.InputStream;

            import java.io.OutputStream;

            import java.net.HttpURLConnection;

            import java.io.BufferedReader;

            import java.io.InputStreamReader;

            import java.net.URL;


            public class Main {
                public static void main(String[] args) throws IOException {
            URL url = new
            URL("https://sdk-0X.moengage.com/v1/experiences/fetch");

            HttpURLConnection httpConn = (HttpURLConnection)
            url.openConnection();

            httpConn.setRequestMethod(“POST”);


            httpConn.setRequestProperty(“Authorization”, “Basic
            XCDEAjFOVk0xQjJKRUs4VVNCUzFICCCCOlVMWkI1Z3JUTTRRWmJJU0RsOFFEM0c2Vw==”);

            httpConn.setRequestProperty(“Content-Type”, “application/json”);

            httpConn.setRequestProperty("MOE-APPKEY", "<Your Workspace ID>");


            httpConn.setDoOutput(true);

            String payload = "{

            "identifiers": {
                "customer_id": "Unique identifier for the user mapped with ID field",
                "user_identifiers": {
                    "u_em": "User Email",
                    "u_mb": "Mobile Number"
                }
            },
                "experience_key": ["<experience-key>"],
                "Custom_attribute":"Example: utm_medium: email",
                "DAY_OF_THE_WEEK": "Day of the week as a string in format: Monday, Tuesday etc.",
                "TIME_OF_THE_DAY": "Time of the day as string: 00,01,02,..,23",
                "USER_IP_ADDRESS": "IP address for location based experiences",
                "USER_AGENT": "Copy the USER-AGENT http header from the request"
            }";


            httpConn.setDoOutput(true);

            OutputStream os = httpConn.getOutputStream();

            os.write(payload.getBytes());

            os.flush();


            InputStream responseStream = httpConn.getResponseCode() ==
            HttpURLConnection.HTTP_OK
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                (responseStream)));

            String output;

            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }


            httpConn.disconnect();

                }
            }
components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
      description: >
        Authentication is done via Basic Auth. This requires a base64-encoded
        string of your credentials in the format 'username:password'.


        - **Username**: Use your MoEngage workspace ID (also known as the App
        ID). You can find it in the MoEngage dashboard at **Settings** >
        **Account** > **APIs** > **Workspace ID (earlier app id)**.

        - **Password**: Use your API Key, which you can find within the
        **Personalize** tile.


        **Note**: After you generate and save the Personalize API Key (SECRET
        KEY), DO NOT generate a new key unless there is a security breach. After
        you generate a different key and save it, API calls using the older key
        won't work.


        For more information on authentication and getting your credentials,
        refer
        [here](https://www.moengage.com/docs/api/introduction#getting-your-credentials).

````