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

# Execute Function

> Cloud Script is one of PlayFab's most versatile features. It allows client code to request execution of any kind of custom server-side functionality you can implement, and it can be used in conjunction with virtually anything.

## Error codes

This operation may return the following PlayFab errors:

| Error | Code |
| --- | --- |
| CloudScriptAzureFunctionsArgumentSizeExceeded | 1471 |
| CloudScriptAzureFunctionsEventHubRequestError | 1579 |
| CloudScriptAzureFunctionsExecutionTimeLimitExceeded | 1470 |
| CloudScriptAzureFunctionsHTTPRequestError | 1473 |
| CloudScriptAzureFunctionsQueueRequestError | 1494 |
| CloudScriptAzureFunctionsReturnSizeExceeded | 1472 |
| InvalidEntityType | 1373 |




## OpenAPI

````yaml /services/playfab/api-references/rest/cloudscript/cloudscript.openapi.json post /CloudScript/ExecuteFunction
openapi: 3.0.0
info:
  version: '260922'
  title: PlayFab CloudScript API
  description: API methods for executing CloudScript using an Entity Profile
  termsOfService: https://playfab.com/terms/
  contact:
    url: https://community.playfab.com/index.html
  license:
    name: Apache 2.0
    url: https://github.com/PlayFab/API_Specs/blob/master/LICENSE
servers:
  - url: https://{titleId}.playfabapi.com
    description: PlayFab title endpoint
    variables:
      titleId:
        default: your_title_id
        description: Your PlayFab title ID (hex).
security: []
tags:
  - name: Server-Side Cloud Script
    description: Server-Side Cloud Script APIs
paths:
  /CloudScript/ExecuteFunction:
    post:
      tags:
        - Server-Side Cloud Script
      summary: Execute Function
      description: >
        Cloud Script is one of PlayFab's most versatile features. It allows
        client code to request execution of any kind of custom server-side
        functionality you can implement, and it can be used in conjunction with
        virtually anything.


        ## Error codes


        This operation may return the following PlayFab errors:


        | Error | Code |

        | --- | --- |

        | CloudScriptAzureFunctionsArgumentSizeExceeded | 1471 |

        | CloudScriptAzureFunctionsEventHubRequestError | 1579 |

        | CloudScriptAzureFunctionsExecutionTimeLimitExceeded | 1470 |

        | CloudScriptAzureFunctionsHTTPRequestError | 1473 |

        | CloudScriptAzureFunctionsQueueRequestError | 1494 |

        | CloudScriptAzureFunctionsReturnSizeExceeded | 1472 |

        | InvalidEntityType | 1373 |
      operationId: ExecuteFunction
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExecuteFunctionRequest'
        description: >-
          Executes an Azure Function with the profile of the entity that is
          defined in the request.
      responses:
        '200':
          $ref: '#/components/responses/ExecuteFunctionResult'
        '400':
          $ref: '#/components/responses/ApiErrorWrapper'
      security:
        - EntityToken: []
components:
  schemas:
    ExecuteFunctionRequest:
      description: >-
        Executes an Azure Function with the profile of the entity that is
        defined in the request.
      type: object
      properties:
        CustomTags:
          description: >-
            The optional custom tags associated with the request (e.g. build
            number, external trace identifiers, etc.).
          type: object
        Entity:
          allOf:
            - $ref: '#/components/schemas/EntityKey'
          description: >-
            The optional entity to perform this action on. Defaults to the
            currently logged in entity.
        FunctionName:
          description: The name of the CloudScript function to execute
          type: string
        FunctionParameter:
          description: >-
            Object that is passed in to the function as the FunctionArgument
            field of the FunctionExecutionContext data structure
          type: object
        GeneratePlayStreamEvent:
          description: >-
            Generate a 'entity_executed_cloudscript_function' PlayStream event
            containing the results of the function execution and other
            contextual information. This event will show up in the PlayStream
            debugger console for the player in Game Manager.
          type: boolean
      required:
        - FunctionName
      example:
        FunctionName: LevelCompleted
        FunctionParameter:
          level: 3
          points: 400
        GeneratePlayStreamEvent: true
        Entity:
          Id: aaa
          Type: title_player_account
          TypeString: title_player_account
    EntityKey:
      description: >-
        Combined entity type and ID structure which uniquely identifies a single
        entity.
      type: object
      properties:
        Id:
          description: Unique ID of the entity.
          type: string
        Type:
          description: >-
            Entity type. See
            https://learn.microsoft.com/gaming/playfab/features/data/entities/available-built-in-entity-types
          type: string
      required:
        - Id
    ExecuteFunctionResult:
      type: object
      properties:
        Error:
          allOf:
            - $ref: '#/components/schemas/FunctionExecutionError'
          description: Error from the CloudScript Azure Function.
        ExecutionTimeMilliseconds:
          description: The amount of time the function took to execute
          type: number
          x-actualtype: int32
        FunctionName:
          description: The name of the function that executed
          type: string
        FunctionResult:
          description: The object returned from the function, if any
          type: object
        FunctionResultSize:
          description: The size in bytes of the object returned from the function, if any
          type: number
          x-actualtype: int32
        FunctionResultTooLarge:
          description: >-
            Flag indicating if the FunctionResult was too large and was
            subsequently dropped from this event.
          type: boolean
      required:
        - ExecutionTimeMilliseconds
      example:
        ExecutionTimeMilliseconds: 367
        FunctionName: LevelCompleted
        FunctionResult:
          newScore: 25
        FunctionResultSize: 43
    ApiErrorWrapper:
      description: The basic wrapper around every failed API response
      type: object
      properties:
        code:
          description: Numerical HTTP code
          type: integer
        status:
          description: String HTTP code
          type: string
        error:
          description: Playfab error code
          type: string
        errorCode:
          description: Numerical PlayFab error code
          type: integer
        errorMessage:
          description: Description for the PlayFab errorCode
          type: string
        errorDetails:
          description: Detailed description of individual issues with the request object
          type: object
      required:
        - code
        - errorCode
    FunctionExecutionError:
      type: object
      properties:
        Error:
          description: >-
            Error code, such as
            CloudScriptAzureFunctionsExecutionTimeLimitExceeded,
            CloudScriptAzureFunctionsArgumentSizeExceeded,
            CloudScriptAzureFunctionsReturnSizeExceeded or
            CloudScriptAzureFunctionsHTTPRequestError
          type: string
        Message:
          description: Details about the error
          type: string
        StackTrace:
          description: >-
            Point during the execution of the function at which the error
            occurred, if any
          type: string
  responses:
    ExecuteFunctionResult:
      description: ''
      content:
        application/json:
          schema:
            type: object
            properties:
              code:
                type: integer
                description: >-
                  The Http status code. If X-ReportErrorAsSuccess header is set
                  to true, this will report the actual http error code.
              status:
                type: string
                description: The Http status code as a string.
              data:
                $ref: '#/components/schemas/ExecuteFunctionResult'
            example:
              code: 200
              status: OK
              data:
                ExecutionTimeMilliseconds: 367
                FunctionName: LevelCompleted
                FunctionResult:
                  newScore: 25
                FunctionResultSize: 43
    ApiErrorWrapper:
      description: This is the outer wrapper for all responses with errors
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ApiErrorWrapper'
  securitySchemes:
    EntityToken:
      type: apiKey
      in: header
      name: X-EntityToken
      description: >-
        This API requires an Entity Session Token, available from the Entity
        GetEntityToken method.

````

## Related topics

- [Execute Function](/services/playfab/api-references/rest/cloudscript/server-side-cloud-script/execute-function.md)
- [ExecuteFunctionResult](/services/playfab/api-references/events/data-types/executefunctionresult.md)
- [PFCloudScriptExecuteFunctionRequest](/services/playfab/api-references/c/pfcloudscripttypes/structs/pfcloudscriptexecutefunctionrequest.md)
- [PFCloudScriptExecuteFunctionAsync](/services/playfab/api-references/c/pfcloudscript/functions/pfcloudscriptexecutefunctionasync.md)
- [PFCloudScriptExecuteFunctionResult](/services/playfab/api-references/c/pfcloudscripttypes/structs/pfcloudscriptexecutefunctionresult.md)
