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

# Export Tokenset

> Export all modes of a tokenset in various formats

# Export Tokenset

Exports all modes of a tokenset in your chosen format. This is useful when you want to download a complete tokenset with all its variations.

<Note>
  This endpoint is part of the API-token surface (`/api/v1`) and does not accept an environment selector.
  For environment-based exports (active release per org environment), use:
  `/api/organizations/{orgId}/environments/{environmentId}/export`.
</Note>

## Endpoint

```
GET /tokensets/{id}/export?format={format}
```

## Request

### Path Parameters

| Parameter | Type   | Required | Description |
| --------- | ------ | -------- | ----------- |
| `id`      | string | Yes      | Tokenset ID |

### Query Parameters

| Parameter | Type   | Required | Description                     |
| --------- | ------ | -------- | ------------------------------- |
| `format`  | string | No       | Export format (default: `json`) |

### Supported Formats

| Format    | Description                            | Extension |
| --------- | -------------------------------------- | --------- |
| `json`    | W3C Design Tokens with mode extensions | `.json`   |
| `css`     | CSS variables (separated by mode)      | `.css`    |
| `ios`     | Swift code for iOS                     | `.swift`  |
| `android` | XML resources for Android              | `.xml`    |

## Response

The response content type and structure depend on the chosen format.

### JSON Format

**Content-Type:** `application/json`

```json theme={null}
{
  "$modes": {
    "light": {
      "colors": {
        "primary": {
          "500": {
            "value": "#3b82f6",
            "type": "color"
          }
        }
      }
    },
    "dark": {
      "colors": {
        "primary": {
          "500": {
            "value": "#60a5fa",
            "type": "color"
          }
        }
      }
    }
  }
}
```

### CSS Format

**Content-Type:** `text/css`

```css theme={null}
/* Light Mode */
:root[data-theme="light"] {
  --colors-primary-500: #3b82f6;
}

/* Dark Mode */
:root[data-theme="dark"] {
  --colors-primary-500: #60a5fa;
}
```

### iOS Format

**Content-Type:** `text/plain`

```swift theme={null}
import UIKit

enum DesignTokens {
    enum Light {
        enum Color {
            static let colorsPrimary500 = UIColor(red: 0.231, green: 0.510, blue: 0.965, alpha: 1.000)
        }
    }
    
    enum Dark {
        enum Color {
            static let colorsPrimary500 = UIColor(red: 0.376, green: 0.647, blue: 0.980, alpha: 1.000)
        }
    }
}
```

### Android Format

**Content-Type:** `application/xml`

```xml theme={null}
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Light Mode -->
    <color name="colors-primary-500-light">#3b82f6</color>
    
    <!-- Dark Mode -->
    <color name="colors-primary-500-dark">#60a5fa</color>
</resources>
```

## Examples

<CodeGroup>
  ```bash JSON theme={null}
  curl -H "Authorization: Bearer tkc_your_token_here" \
    "https://app.tokencraft.dev/api/v1/tokensets/tokenset-123/export?format=json" \
    -o tokens.json
  ```

  ```bash CSS theme={null}
  curl -H "Authorization: Bearer tkc_your_token_here" \
    "https://app.tokencraft.dev/api/v1/tokensets/tokenset-123/export?format=css" \
    -o tokens.css
  ```

  ```bash iOS theme={null}
  curl -H "Authorization: Bearer tkc_your_token_here" \
    "https://app.tokencraft.dev/api/v1/tokensets/tokenset-123/export?format=ios" \
    -o DesignTokens.swift
  ```

  ```bash Android theme={null}
  curl -H "Authorization: Bearer tkc_your_token_here" \
    "https://app.tokencraft.dev/api/v1/tokensets/tokenset-123/export?format=android" \
    -o tokens.xml
  ```
</CodeGroup>

### JavaScript Example

```javascript theme={null}
async function exportTokenset(tokensetId, format = 'json') {
  const response = await fetch(
    `https://app.tokencraft.dev/api/v1/tokensets/${tokensetId}/export?format=${format}`,
    {
      headers: {
        'Authorization': `Bearer ${process.env.TOKENCRAFT_TOKEN}`
      }
    }
  );
  
  if (format === 'json') {
    return await response.json();
  }
  
  return await response.text();
}

// Usage
const tokens = await exportTokenset('tokenset-123', 'json');
const css = await exportTokenset('tokenset-123', 'css');
```

## Error Responses

### 400 Bad Request

```json theme={null}
{
  "error": "Invalid format. Supported formats: json, css, ios, android"
}
```

### 404 Not Found

```json theme={null}
{
  "error": "Tokenset not found"
}
```

## Use Cases

### 1. Download Complete Tokenset

Export all modes for local development:

```bash theme={null}
#!/bin/bash
TOKENSET_ID="tokenset-123"
TOKEN="tkc_your_token_here"

curl -H "Authorization: Bearer $TOKEN" \
  "https://app.tokencraft.dev/api/v1/tokensets/$TOKENSET_ID/export?format=json" \
  -o tokens.json

curl -H "Authorization: Bearer $TOKEN" \
  "https://app.tokencraft.dev/api/v1/tokensets/$TOKENSET_ID/export?format=css" \
  -o tokens.css
```

### 2. CI/CD Build Step

Automatically sync tokens during builds:

```yaml theme={null}
# .github/workflows/sync-tokens.yml
- name: Download tokens
  run: |
    curl -H "Authorization: Bearer ${{ secrets.TOKENCRAFT_TOKEN }}" \
      "${{ secrets.API_BASE }}/tokensets/${{ secrets.TOKENSET_ID }}/export?format=css" \
      -o src/styles/tokens.css
```

### 3. Multi-platform Export

Generate platform-specific files:

```javascript theme={null}
async function exportAllPlatforms(tokensetId) {
  const formats = ['json', 'css', 'ios', 'android'];
  
  for (const format of formats) {
    const content = await exportTokenset(tokensetId, format);
    const filename = `tokens.${getExtension(format)}`;
    fs.writeFileSync(filename, content);
    console.log(`Exported ${filename}`);
  }
}
```

## Best Practices

### 1. Cache Exports

Tokens don't change frequently:

```javascript theme={null}
const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes

async function getCachedExport(tokensetId, format) {
  const key = `${tokensetId}-${format}`;
  const cached = cache.get(key);
  
  if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
    return cached.data;
  }
  
  const data = await exportTokenset(tokensetId, format);
  cache.set(key, { data, timestamp: Date.now() });
  
  return data;
}
```

### 2. Automate Syncing

Set up scheduled exports:

```javascript theme={null}
// Run daily at 2 AM
cron.schedule('0 2 * * *', async () => {
  const tokens = await exportTokenset(tokensetId, 'json');
  fs.writeFileSync('tokens.json', JSON.stringify(tokens, null, 2));
  console.log('Tokens synced');
});
```

### 3. Version Control

Commit generated files:

```bash theme={null}
#!/bin/bash
./scripts/sync-tokens.sh

if [[ `git status --porcelain` ]]; then
  git add tokens.json tokens.css
  git commit -m "Update design tokens"
  git push
fi
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Export Single Mode" icon="file" href="/api-reference/export/mode">
    Export a specific mode
  </Card>

  <Card title="GitHub Actions" icon="github" href="/integrations/github-actions">
    Automate token syncing
  </Card>
</CardGroup>
