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

# Server exports

> Upload, list, delete and purge FiveMesh CDN objects from FiveM server scripts.

Use server exports when your FiveM resource needs to manage FiveMesh CDN objects.

The default service key is read from `FIVEMESH_API_KEY`, so these exports should be called from server-side Lua or JavaScript.

## Upload a file

Use `uploadFile` when you already have bytes, a data URL or a `Blob`-compatible value.

<CodeGroup>
  ```lua Lua theme={null}
  local result = exports["fivemesh-sdk"]:uploadFile(fileBytes, {
      filename = "inventory.png",
      path = "inventory/items",
      metadata = {
          source = "inventory"
      }
  })

  print(result.object.publicUrl)
  ```

  ```js JavaScript theme={null}
  const result = await exports["fivemesh-sdk"].uploadFile(fileBytes, {
    filename: "inventory.png",
    path: "inventory/items",
    metadata: {
      source: "inventory",
    },
  });

  console.log(result.object.publicUrl);
  ```
</CodeGroup>

Upload responses include the stored object and its `publicUrl`.

```json theme={null}
{
  "success": true,
  "object": {
    "key": "inventory/items/inventory.png",
    "size": 12421,
    "contentType": "image/png",
    "publicUrl": "https://cdn.fivemesh.io/workspace/inventory/items/inventory.png"
  },
  "requestId": "req_..."
}
```

## Upload an image data URL

Use `uploadImage` when a script gives you an image as a data URL.

```lua theme={null}
local result = exports["fivemesh-sdk"]:uploadImage(dataUrl, {
    playerSource = source
}, {
    filename = "profile.webp",
    path = "screenshots/profile"
})

print(result.object.publicUrl)
```

## Use an API key profile

Pass `keyProfile` when an export should use a named key such as `FIVEMESH_API_KEY_MUGSHOTS` instead of the default `FIVEMESH_API_KEY`.

```lua theme={null}
local result = exports["fivemesh-sdk"]:uploadImage(dataUrl, {
    playerSource = source
}, {
    keyProfile = "MUGSHOTS",
    filename = "profile.webp",
    path = "screenshots/profile"
})
```

The profile name is case-sensitive and is appended directly to `FIVEMESH_API_KEY_`.

## Upload multiple files

Use `bulkUpload` when a resource needs to upload a small batch and handle per-file results.

```lua theme={null}
local result = exports["fivemesh-sdk"]:bulkUpload({
    {
        data = firstFileBytes,
        filename = "a.png",
        path = "inventory/items"
    },
    {
        data = secondFileBytes,
        filename = "b.png",
        path = "inventory/items"
    }
})

for _, item in ipairs(result.results) do
    if item.success then
        print(item.publicUrl)
    else
        print(("Upload failed for %s: %s"):format(item.fieldName, item.error))
    end
end
```

## List CDN objects

Use `listObjects` with a path and optional pagination limit.

```lua theme={null}
local result = exports["fivemesh-sdk"]:listObjects({
    path = "screenshots",
    limit = 50
})

for _, object in ipairs(result.objects) do
    print(object.key, object.publicUrl)
end
```

If `continuationToken` is returned, pass it to the next call.

```lua theme={null}
local nextPage = exports["fivemesh-sdk"]:listObjects({
    path = "screenshots",
    limit = 50,
    continuationToken = result.continuationToken
})
```

## Delete objects

Use `deleteObject` for one path or `bulkDelete` for a batch.

```lua theme={null}
exports["fivemesh-sdk"]:deleteObject("screenshots/old.webp")

local result = exports["fivemesh-sdk"]:bulkDelete({
    "screenshots/a.webp",
    "screenshots/b.webp"
})
```

Bulk delete returns one result per path, so your script can log partial failures.

```lua theme={null}
for _, item in ipairs(result.results) do
    if not item.success then
        print(("Failed to delete %s: %s"):format(item.path, item.error))
    end
end
```

## Purge CDN files

Use `purgeObjects` when a resource needs Cloudflare to refresh public CDN files after an overwrite or external change. The paths are bucket-relative and must be allowed by the service key.

<CodeGroup>
  ```lua Lua theme={null}
  local result = exports["fivemesh-sdk"]:purgeObjects({
      "screenshots/a.webp",
      "screenshots/b.webp"
  })

  print(("Purged %d CDN files"):format(result.purged))
  ```

  ```js JavaScript theme={null}
  const result = await exports["fivemesh-sdk"].purgeObjects([
    "screenshots/a.webp",
    "screenshots/b.webp",
  ]);

  console.log(`Purged ${result.purged} CDN files`);
  ```
</CodeGroup>

The service key needs `cdn:purge` for this export.

## Export reference

| Export                                    | Description                                                 |
| ----------------------------------------- | ----------------------------------------------------------- |
| `listObjects(options)`                    | Lists objects under an allowed CDN path.                    |
| `uploadFile(data, options)`               | Uploads one file to FiveMesh CDN.                           |
| `uploadImage(dataUrl, metadata, options)` | Uploads an image data URL.                                  |
| `bulkUpload(items, options)`              | Uploads multiple files and returns per-file results.        |
| `deleteObject(path, options)`             | Deletes one object path.                                    |
| `bulkDelete(paths, options)`              | Deletes multiple object paths and returns per-path results. |
| `purgeObjects(paths, options)`            | Purges public CDN cache for one or more object paths.       |

## Related pages

* [Installation](/sdk/installation)
* [Screenshots](/sdk/screenshots)
* [FiveMesh CDN API](/api-reference/cdn)
