File size: 6,012 Bytes
e735481 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | # Sweety.dev API Documentation
Base URL: `https://your-space.hf.space`
Storage: `/data` (HF Spaces persistent storage)
---
## Build APIs
### POST /api/build
Create a new build for a React project.
**Request Body:**
```json
{
"user_id": "user123",
"project_id": "proj456", // optional, auto-generated if omitted
"files": {
"src/App.jsx": "export default function App() { return <div>Hello</div> }",
"src/components/Button.jsx": "export const Button = () => <button>Click</button>",
"public/logo.png": "base64_encoded_image"
},
"dependencies": {
"lucide-react": "^0.300",
"framer-motion": "^10.0"
},
"framework": "react"
}
```
**Response:**
```json
{
"project_id": "proj456",
"user_id": "user123",
"status": "pending"
}
```
**Build Status Values:**
- `pending` - Build queued
- `installing` - npm install running
- `building` - vite build running
- `success` - Build complete, preview ready
- `failed` - Build failed (check error field)
---
### GET /api/status/{user_id}/{project_id}
Check build status.
**Response:**
```json
{
"project_id": "proj456",
"user_id": "user123",
"status": "success",
"url": "/preview/user123/proj456/",
"error": null
}
```
---
### DELETE /api/build/{user_id}/{project_id}
Delete a build and its artifacts.
**Response:**
```json
{
"deleted": true
}
```
---
## File Management APIs
### POST /api/file
Create or upload a file to user storage.
**Request Body:**
```json
{
"user_id": "user123",
"project_id": "proj456",
"filename": "src/components/Header.jsx",
"content": "export const Header = () => <header>Header</header>"
}
```
**Response:**
```json
{
"created": true,
"path": "/data/users/user123/files/src/components/Header.jsx"
}
```
---
### GET /api/file/{user_id}/{filename:path}
Read a file from user storage.
**Response:**
```json
{
"content": "export const Header = () => <header>Header</header>"
}
```
---
### PUT /api/file
Update an existing file.
**Request Body:**
```json
{
"user_id": "user123",
"project_id": "proj456",
"filename": "src/components/Header.jsx",
"content": "export const Header = () => <header>New Header</header>"
}
```
**Response:**
```json
{
"updated": true
}
```
---
### DELETE /api/file/{user_id}/{filename:path}
Delete a file.
**Response:**
```json
{
"deleted": true
}
```
---
### GET /api/files/{user_id}
List all files for a user.
**Response:**
```json
[
{
"filename": "src/App.jsx",
"size": 1234
},
{
"filename": "src/components/Header.jsx",
"size": 567
}
]
```
---
## Project Management APIs
### GET /api/projects/{user_id}
List all projects for a user.
**Response:**
```json
[
{
"project_id": "proj456",
"files": ["src/App.jsx", "src/components/Button.jsx"],
"dependencies": {"lucide-react": "^0.300"},
"created_at": "123456.789"
},
{
"project_id": "proj789",
"files": ["src/App.jsx"],
"dependencies": {},
"created_at": "123457.890"
}
]
```
---
## Preview
### GET /preview/{user_id}/{project_id}/{path:path}
Serve built static files.
**Examples:**
- `/preview/user123/proj456/` - Main index.html
- `/preview/user123/proj456/assets/index.abc123.js` - JavaScript bundle
- `/preview/user123/proj456/assets/index.def456.css` - CSS
**Note:** SPA routing automatically falls back to index.html for non-existent paths.
---
## Storage Structure
```
/data/
βββ users/
β βββ {user_id}/
β βββ files/ # User file storage
β β βββ src/
β β β βββ App.jsx
β β β βββ components/
β β β βββ Header.jsx
β β βββ public/
β β βββ logo.png
β βββ projects/ # Project metadata
β βββ proj456.json
β βββ proj789.json
βββ builds/
βββ {user_id}/
βββ {project_id}/ # React build artifacts
βββ src/
βββ package.json
βββ vite.config.js
βββ index.html
βββ dist/ # Built static files
βββ index.html
βββ assets/
```
---
## Example Usage
### 1. Create a Build
```javascript
const response = await fetch('/api/build', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
user_id: 'user123',
files: {
'src/App.jsx': `export default function App() {
return (
<div className="p-8">
<h1>Hello Sweety!</h1>
</div>
)
}`,
'src/components/Button.jsx': `export const Button = () => <button>Click</button>`
},
dependencies: {
'lucide-react': '^0.300'
}
})
});
const { project_id, user_id, status } = await response.json();
```
### 2. Poll Build Status
```javascript
const checkStatus = async () => {
const res = await fetch(`/api/status/${user_id}/${project_id}`);
const data = await res.json();
if (data.status === 'success') {
console.log('Preview ready:', data.url);
// Load in iframe: <iframe src={data.url} />
} else if (data.status === 'failed') {
console.error('Build failed:', data.error);
}
};
setInterval(checkStatus, 1000);
```
### 3. Store Files Before Build
```javascript
// Save individual files
await fetch('/api/file', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
user_id: 'user123',
project_id: 'proj456',
filename: 'src/App.jsx',
content: 'export default function App() { return <div>Hello</div> }'
})
});
// List files
const files = await fetch(`/api/files/${user_id}`).then(r => r.json());
```
---
## Rate Limits & Concurrency
- **Concurrent Builds:** 5 per user
- **File Size:** 10MB per file
- **Build Timeout:** 2 minutes
- **Storage:** 1GB per user (HF Spaces limit)
Upgrade to CPU Upgrade for higher limits and faster builds.
|