# Server Exports

> Set custom app policies and send messages or notifications to a player's app from server Lua.

[All exports](/docs/scripts/free-phone/exports) · [Client app registration](/docs/scripts/free-phone/exports/client-exports)

Call these exports in server Lua after [Phone is ready](/docs/scripts/free-phone/events#api-readiness). Use the same resource and app ID for the policy and the client definition. Validate permissions, target players, and input before acting on any player request.

## Server Exports

### Custom-app policies

Client definitions and server policies are separate. A client definition cannot grant itself
server-side permissions.

### `AddCustomAppPolicy(definition)`

- **Purpose**: Registers a server policy owned by the calling resource.
- **Arguments**:

  - `definition` (`table`): Policy definition.
  
    - `id` (`string`): Matching custom-app ID. Required.
    - `permissions` (`table`): Server-approved permissions.
- **Returns**: `boolean, string?`.

```lua [server.lua]
local registered, error_code = exports["sky_phone"]:AddCustomAppPolicy({
    id = "dispatch-board",
    permissions = {
        "device.storage",
        "notifications",
    },
})

if not registered then
    error(("[my_dispatch] Policy registration failed: %s"):format(error_code))
end
```

### `UpdateCustomAppPolicy(definition)`

- **Purpose**: Replaces the complete policy owned by the calling resource.
- **Arguments**:

  - `definition` (`table`): Full policy including `id` and every allowed permission.
- **Returns**: `boolean, string?`.

```lua [server.lua]
local updated, error_code = exports["sky_phone"]:UpdateCustomAppPolicy({
    id = "dispatch-board",
    permissions = {
        "device.storage",
        "notifications",
        "notifications.critical",
    },
})

if not updated then
    print(("[my_dispatch] Policy update failed: %s"):format(error_code))
end
```

### `RemoveCustomAppPolicy(appId)`

- **Purpose**: Removes a policy owned by the calling resource.
- **Arguments**:

  - `appId` (`string`): Registered app ID.
- **Returns**: `boolean, string?`.

```lua [server.lua]
local removed, error_code = exports["sky_phone"]:RemoveCustomAppPolicy("dispatch-board")

if not removed then
    print(("[my_dispatch] Policy removal failed: %s"):format(error_code))
end
```

### `GetCustomAppPolicy(appId)`

- **Purpose**: Reads the normalized server policy for an app.
- **Arguments**:

  - `appId` (`string`): App ID.
- **Returns**: `table | nil`.

```lua [server.lua]
local policy = exports["sky_phone"]:GetCustomAppPolicy("dispatch-board")

if policy then
    print("Owner", policy.ownerResource)
    for _, permission in ipairs(policy.permissions or {}) do
        print("Permission", permission)
    end
end
```

Treat the returned table as a snapshot. Mutating it does not update the registered policy.

### `HasCustomAppPermission(appId, permission)`

- **Purpose**: Checks whether a registered policy contains one permission.
- **Arguments**:

  - `appId` (`string`): App ID.
  - `permission` (`string`): Supported permission name.
- **Returns**: `boolean`.

```lua [server.lua]
local can_store = exports["sky_phone"]:HasCustomAppPermission(
    "dispatch-board",
    "device.storage"
)

if not can_store then
    print("Dispatch Board has no storage permission")
end
```

### `GetCustomAppCapabilities()`

- **Purpose**: Reads the server-side custom-app ABI and policy capabilities.
- **Returns**: `table`.

```lua [server.lua]
local capabilities = exports["sky_phone"]:GetCustomAppCapabilities()

print("External apps enabled", capabilities.enabled)
print("ABI", capabilities.abiVersion)
```

### `SendCustomAppMessage(playerSource, appId, payload)`

Dispatches JSON-compatible data to an online player's client for an app with a policy owned by the calling resource. The payload must fit the configured message limit. The return value is `true` when the message was dispatched, or `false, errorCode` on failure. Use the iframe bridge to receive the data in an open app.

```lua [server.lua]
local sent, error_code = exports.sky_phone:SendCustomAppMessage(player_source, "dispatch-board", {
    type = "incidents:refresh",
})

if not sent then
    print(("Dispatch message failed: %s"):format(error_code))
end
```

### `SendCustomAppNotification(playerSource, appId, notification)`

Sends a notification to a player's phone. The calling resource must own the policy and grant `notifications`. Pass a table with the notification `text` and optional `title`. The export returns `true` with the notification result when delivered to the target, or `false, errorCode` when it cannot deliver it.

```lua [server.lua]
local sent, result = exports.sky_phone:SendCustomAppNotification(player_source, "dispatch-board", {
    title = "Dispatch",
    text = "Your next assignment is available.",
})

if not sent then
    print(("Dispatch notification failed: %s"):format(result))
end
```
