# Apple FairPlay DRM

> Part of the [SpatialGen API documentation](/index.md). [HTML version of this page](/security/fairplay-drm/).

Implementing Apple FairPlay DRM with SpatialGen

## API context

-   DRM base URL: `https://drm.spatialgen.com`.
-   Authenticate requests to SpatialGen API endpoints with the `X-SPATIALGEN-APPKEY` HTTP header.
-   Use a key from the workspace that owns the requested resources. Follow the roles and scopes specified for each endpoint below.
-   Keep `admin` and `upload` keys on a trusted backend. `readonly` keys may be used in client apps for permitted read operations.
-   Send DRM key and license requests through your backend, keeping the SpatialGen API key there.

See the [API key guide](/guides/get-started.md) for roles, key creation, expiration, revocation, and rotation.

* * *

## Getting Started

SpatialGen has direct integration of Apple’s FairPlay DRM to secure your video content. Reach out to the team at [contact@spatialgen.com](mailto://contact@spatialgen.com) and they can help you add FairPlay to your account.

## Fairplay Streaming Workflow

During playback, your app will need to request the decryption keys for your videos. SpatialGen DRM is hosted at `drm.spatialgen.com`, and all requests in the FairPlay workflow will be made to this url.

The FairPlay workflow requires two pieces of information from your app. When the HLS Stream is first requested, the playlist (.m3u8) file will contain an **AssetId** to identify the stream. Next, when playback of a FairPlay video begins, the devices’ Operating System (e.g. VisionOS or iOS), will generate a **Server Playback Context (SPC)** based on the AssetId.

Both the AssetId and SPC will be sent to the SpatialGen DRM server where the decryption key will be generated and returned as a **Content Key Context (CKC)** which your App will use to play the encrypted video. Learn more about the FairPlay Streaming workflow at [developer.apple.com](https://developer.apple.com/streaming/fps/FairPlayStreamingOverview.pdf).

### Requsting CKCs

**POST** `/fairplay/spc`

Send a POST request to the SpatialGen DRM server with the SPC and AssetId to receive the CKC. The body of the post request should contain the SPC and AssetId in JSON format. The SPC should be sent as a _base64 encoded string_.

A successful request will return a JSON object containing the CKC as a base64 encoded string. Your app will be responsible for decoding the CKC into a raw Buffer to use as the decryption key.

#### Example Request

fetchFairplayCKC.js

```js
// Example data to send
const data = {
  asset_id: "<asset_id>",
  spc: "<base64 encoded SPC>"
};

// Fetch call
fetch('https://drm.spatialgen.com/fairplay/spc', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-SPATIALGEN-APPKEY': apiKey
  },
  body: JSON.stringify(data)
})
```

#### Example Response

response

```json
{
  ckc: "<base64 encoded CKC>"
}
```
