Upload 2 files
Browse files- API_GUIDE.md +20 -16
- PRIVATE_SPACE_GUIDE.md +11 -18
API_GUIDE.md
CHANGED
@@ -1,33 +1,37 @@
|
|
1 |
# API Authentication & Usage Guide
|
2 |
|
3 |
-
## 🔐 Setting Up API
|
4 |
|
5 |
-
### Step 1: Generate Secure API
|
6 |
```bash
|
7 |
-
# Generate random API
|
8 |
-
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
|
9 |
-
# Or use
|
10 |
-
|
11 |
```
|
12 |
|
13 |
### Step 2: Configure in HF Spaces
|
14 |
1. Navigate to your Space: `https://huggingface.co/spaces/your-username/space-name`
|
15 |
2. Click **"Settings"** tab
|
16 |
3. Click **"Variables"** section
|
17 |
-
4. Add
|
18 |
|
19 |
| Variable | Value | Description |
|
20 |
|----------|-------|-------------|
|
21 |
-
| `
|
22 |
-
| `REQUIRE_API_KEY` | `true` | Enable authentication |
|
23 |
|
24 |
### Step 3: Test Authentication
|
25 |
```bash
|
26 |
-
# Test without API key (should fail)
|
27 |
-
curl https://your-space.hf.space/screenshot
|
|
|
|
|
28 |
|
29 |
# Test with API key (should work)
|
30 |
-
curl -
|
|
|
|
|
|
|
31 |
```
|
32 |
|
33 |
## 📊 Understanding Server Status Responses
|
@@ -71,7 +75,7 @@ async function screenshotWithRetry(url, options = {}, maxRetries = 3) {
|
|
71 |
method: 'POST',
|
72 |
headers: {
|
73 |
'Content-Type': 'application/json',
|
74 |
-
'
|
75 |
},
|
76 |
body: JSON.stringify({ url, ...options })
|
77 |
});
|
@@ -111,7 +115,7 @@ def screenshot_with_retry(url: str, api_key: str, max_retries: int = 3) -> Optio
|
|
111 |
try:
|
112 |
response = requests.post(
|
113 |
'https://your-space.hf.space/screenshot',
|
114 |
-
headers={'
|
115 |
json={'url': url},
|
116 |
timeout=30
|
117 |
)
|
@@ -186,7 +190,7 @@ class ScreenshotAPI {
|
|
186 |
...options,
|
187 |
headers: {
|
188 |
'Content-Type': 'application/json',
|
189 |
-
'
|
190 |
...options.headers
|
191 |
}
|
192 |
});
|
@@ -200,7 +204,7 @@ class ScreenshotAPI {
|
|
200 |
|
201 |
| Error | Cause | Solution |
|
202 |
|-------|-------|----------|
|
203 |
-
| `401 Unauthorized` | Missing API key | Add `
|
204 |
| `403 Forbidden` | Invalid API key | Check key spelling/validity |
|
205 |
| `503 Service Unavailable` | Server overloaded | Implement retry with delay |
|
206 |
| `429 Too Many Requests` | Rate limit exceeded | Wait for reset time |
|
|
|
1 |
# API Authentication & Usage Guide
|
2 |
|
3 |
+
## 🔐 Setting Up API Key in Hugging Face Spaces
|
4 |
|
5 |
+
### Step 1: Generate Secure API Key
|
6 |
```bash
|
7 |
+
# Generate random API key (example methods)
|
8 |
+
node -e "console.log('sk-' + require('crypto').randomBytes(32).toString('hex'))"
|
9 |
+
# Or use Python
|
10 |
+
python -c "import secrets; print('sk-' + secrets.token_hex(32))"
|
11 |
```
|
12 |
|
13 |
### Step 2: Configure in HF Spaces
|
14 |
1. Navigate to your Space: `https://huggingface.co/spaces/your-username/space-name`
|
15 |
2. Click **"Settings"** tab
|
16 |
3. Click **"Variables"** section
|
17 |
+
4. Add this environment variable:
|
18 |
|
19 |
| Variable | Value | Description |
|
20 |
|----------|-------|-------------|
|
21 |
+
| `API_KEY` | `sk-your-secure-key-here` | Single API key for authentication |
|
|
|
22 |
|
23 |
### Step 3: Test Authentication
|
24 |
```bash
|
25 |
+
# Test without API key (should fail if authentication enabled)
|
26 |
+
curl -X POST https://your-space.hf.space/screenshot \
|
27 |
+
-H "Content-Type: application/json" \
|
28 |
+
-d '{"url": "https://example.com"}'
|
29 |
|
30 |
# Test with API key (should work)
|
31 |
+
curl -X POST https://your-space.hf.space/screenshot \
|
32 |
+
-H "Authorization: Bearer your-api-key-here" \
|
33 |
+
-H "Content-Type: application/json" \
|
34 |
+
-d '{"url": "https://example.com"}'
|
35 |
```
|
36 |
|
37 |
## 📊 Understanding Server Status Responses
|
|
|
75 |
method: 'POST',
|
76 |
headers: {
|
77 |
'Content-Type': 'application/json',
|
78 |
+
'Authorization': 'Bearer your-api-key'
|
79 |
},
|
80 |
body: JSON.stringify({ url, ...options })
|
81 |
});
|
|
|
115 |
try:
|
116 |
response = requests.post(
|
117 |
'https://your-space.hf.space/screenshot',
|
118 |
+
headers={'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json'},
|
119 |
json={'url': url},
|
120 |
timeout=30
|
121 |
)
|
|
|
190 |
...options,
|
191 |
headers: {
|
192 |
'Content-Type': 'application/json',
|
193 |
+
'Authorization': `Bearer ${this.apiKey}`,
|
194 |
...options.headers
|
195 |
}
|
196 |
});
|
|
|
204 |
|
205 |
| Error | Cause | Solution |
|
206 |
|-------|-------|----------|
|
207 |
+
| `401 Unauthorized` | Missing API key | Add `Authorization: Bearer` header |
|
208 |
| `403 Forbidden` | Invalid API key | Check key spelling/validity |
|
209 |
| `503 Service Unavailable` | Server overloaded | Implement retry with delay |
|
210 |
| `429 Too Many Requests` | Rate limit exceeded | Wait for reset time |
|
PRIVATE_SPACE_GUIDE.md
CHANGED
@@ -84,8 +84,7 @@ else:
|
|
84 |
本 API 支持多种认证方式,按以下优先级处理:
|
85 |
|
86 |
1. **Hugging Face Token** (`Authorization: Bearer hf_xxx`) - 最高优先级
|
87 |
-
2. **自定义
|
88 |
-
3. **API Key Header** (`X-API-Key: xxx`)
|
89 |
|
90 |
## 🚨 Token 安全注意事项
|
91 |
|
@@ -119,17 +118,14 @@ else:
|
|
119 |
|
120 |
## 🛠️ 环境变量配置
|
121 |
|
122 |
-
|
123 |
|
124 |
```bash
|
125 |
-
#
|
126 |
-
|
127 |
-
|
128 |
-
# 自定义 API keys(可选)
|
129 |
-
API_KEYS=your-custom-key-1,your-custom-key-2,your-custom-key-3
|
130 |
```
|
131 |
|
132 |
-
**注意:**
|
133 |
|
134 |
## 🔍 调试和监控
|
135 |
|
@@ -143,14 +139,11 @@ curl https://your-username-space-name.hf.space/ \
|
|
143 |
```json
|
144 |
{
|
145 |
"message": "Page Screenshot API - Hugging Face Spaces",
|
146 |
-
"version": "1.
|
147 |
"authentication": {
|
|
|
148 |
"required": true,
|
149 |
-
"
|
150 |
-
"X-API-Key: custom-api-key",
|
151 |
-
"Authorization: Bearer custom-api-key",
|
152 |
-
"Authorization: Bearer hf_token (for private Spaces)"
|
153 |
-
]
|
154 |
}
|
155 |
}
|
156 |
```
|
@@ -176,9 +169,9 @@ curl https://your-username-space-name.hf.space/status \
|
|
176 |
### 示例错误响应
|
177 |
```json
|
178 |
{
|
179 |
-
"error": "
|
180 |
-
"message": "
|
181 |
-
"
|
182 |
}
|
183 |
```
|
184 |
|
|
|
84 |
本 API 支持多种认证方式,按以下优先级处理:
|
85 |
|
86 |
1. **Hugging Face Token** (`Authorization: Bearer hf_xxx`) - 最高优先级
|
87 |
+
2. **自定义 API Key** (`Authorization: Bearer xxx` 或 `x-api-key: xxx`)
|
|
|
88 |
|
89 |
## 🚨 Token 安全注意事项
|
90 |
|
|
|
118 |
|
119 |
## 🛠️ 环境变量配置
|
120 |
|
121 |
+
如果你想启用自定义 API key 认证,可以在 Space 设置中添加:
|
122 |
|
123 |
```bash
|
124 |
+
# 自定义 API key(可选)
|
125 |
+
API_KEY=sk-your-secure-api-key-here
|
|
|
|
|
|
|
126 |
```
|
127 |
|
128 |
+
**注意:** 即使没有设置 `API_KEY`,HF access token 仍然可以正常使用。
|
129 |
|
130 |
## 🔍 调试和监控
|
131 |
|
|
|
139 |
```json
|
140 |
{
|
141 |
"message": "Page Screenshot API - Hugging Face Spaces",
|
142 |
+
"version": "1.4.0",
|
143 |
"authentication": {
|
144 |
+
"type": "API Key Required",
|
145 |
"required": true,
|
146 |
+
"note": "API key required for screenshot endpoint"
|
|
|
|
|
|
|
|
|
147 |
}
|
148 |
}
|
149 |
```
|
|
|
169 |
### 示例错误响应
|
170 |
```json
|
171 |
{
|
172 |
+
"error": "Unauthorized",
|
173 |
+
"message": "Valid API key required. Please provide API key in Authorization header or x-api-key header.",
|
174 |
+
"hint": "Use \"Authorization: Bearer YOUR_API_KEY\" or \"x-api-key: YOUR_API_KEY\""
|
175 |
}
|
176 |
```
|
177 |
|