|
| 1 | +# Substack API Server Guide |
| 2 | + |
| 3 | +HTTP API server that exposes Substack functionality via REST endpoints instead of command-line scripts. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +### 1. Install Dependencies |
| 8 | +```bash |
| 9 | +pip install -r requirements.txt |
| 10 | +``` |
| 11 | + |
| 12 | +### 2. Configure Environment |
| 13 | +```bash |
| 14 | +# Option 1: Use existing command |
| 15 | +python change_env.py |
| 16 | + |
| 17 | +# Option 2: Use API endpoint (after server starts) |
| 18 | +curl -X PUT "http://localhost:8000/environment" \ |
| 19 | + -H "Content-Type: application/json" \ |
| 20 | + -d '{ |
| 21 | + "publication_url": "https://yourname.substack.com", |
| 22 | + "user_id": "your_username", |
| 23 | + "sid": "cookie_value", |
| 24 | + "substack_sid": "cookie_value", |
| 25 | + "substack_lli": "cookie_value" |
| 26 | + }' |
| 27 | +``` |
| 28 | + |
| 29 | +### 3. Start API Server |
| 30 | +```bash |
| 31 | +python api_server.py |
| 32 | +``` |
| 33 | + |
| 34 | +Server runs on: http://localhost:8000 |
| 35 | +Interactive docs: http://localhost:8000/docs |
| 36 | + |
| 37 | +## API Endpoints |
| 38 | + |
| 39 | +### 📝 Create Draft from Markup |
| 40 | +```bash |
| 41 | +POST /drafts/create-markup |
| 42 | +Content-Type: application/json |
| 43 | + |
| 44 | +{ |
| 45 | + "title": "My Post Title", |
| 46 | + "markup_content": "Title:: Hello World | Text:: This is **bold** content | Subscribe:: Join Now", |
| 47 | + "subtitle": "Optional subtitle" |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**Response:** |
| 52 | +```json |
| 53 | +{ |
| 54 | + "success": true, |
| 55 | + "draft_id": 123456, |
| 56 | + "title": "My Post Title", |
| 57 | + "subtitle": "Optional subtitle", |
| 58 | + "url": "https://yourname.substack.com/publish/post/123456", |
| 59 | + "message": "Draft created successfully with 3 content blocks" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### 🧪 Create Test Draft |
| 64 | +```bash |
| 65 | +POST /drafts/create-test |
| 66 | +``` |
| 67 | + |
| 68 | +Creates comprehensive test draft with all content types (headings, lists, quotes, buttons, etc.) |
| 69 | + |
| 70 | +### 📋 List Unpublished Drafts |
| 71 | +```bash |
| 72 | +GET /drafts |
| 73 | +``` |
| 74 | + |
| 75 | +**Response:** |
| 76 | +```json |
| 77 | +[ |
| 78 | + { |
| 79 | + "id": 123456, |
| 80 | + "title": "My Post Title", |
| 81 | + "subtitle": "Optional subtitle", |
| 82 | + "content_preview": "This is bold content...", |
| 83 | + "updated_at": "2024-01-15T10:30:00Z" |
| 84 | + } |
| 85 | +] |
| 86 | +``` |
| 87 | + |
| 88 | +### 🚀 Publish Draft |
| 89 | +```bash |
| 90 | +POST /drafts/{draft_id}/publish |
| 91 | +Content-Type: application/json |
| 92 | + |
| 93 | +{ |
| 94 | + "send_email": true, |
| 95 | + "audience": "everyone" // or "paid" |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +**Response:** |
| 100 | +```json |
| 101 | +{ |
| 102 | + "success": true, |
| 103 | + "post_id": 123456, |
| 104 | + "post_url": "https://yourname.substack.com/p/my-post-title", |
| 105 | + "message": "Draft published successfully" |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### 🔧 Update Environment |
| 110 | +```bash |
| 111 | +PUT /environment |
| 112 | +Content-Type: application/json |
| 113 | + |
| 114 | +{ |
| 115 | + "publication_url": "https://yourname.substack.com", |
| 116 | + "user_id": "your_username", |
| 117 | + "sid": "new_cookie_value" |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### 📖 Get Markup Syntax Guide |
| 122 | +```bash |
| 123 | +GET /markup-syntax |
| 124 | +``` |
| 125 | + |
| 126 | +Returns complete syntax reference for markup formatting. |
| 127 | + |
| 128 | +## Markup Syntax (API) |
| 129 | + |
| 130 | +Same syntax as command-line version: |
| 131 | + |
| 132 | +### Basic Format |
| 133 | +``` |
| 134 | +Type:: Content | Type:: Content | Type:: Content |
| 135 | +``` |
| 136 | + |
| 137 | +### Example API Request |
| 138 | +```bash |
| 139 | +curl -X POST "http://localhost:8000/drafts/create-markup" \ |
| 140 | + -H "Content-Type: application/json" \ |
| 141 | + -d '{ |
| 142 | + "title": "Market Analysis", |
| 143 | + "markup_content": "Title:: Gold Market Update | Text:: Analysis shows **strong performance** this quarter with [key trends](https://example.com) | Quote:: Gold remains the ultimate store of value | List:: • Central bank buying • Inflation hedging • Geopolitical tensions | Subscribe:: Get Market Updates" |
| 144 | + }' |
| 145 | +``` |
| 146 | + |
| 147 | +## Integration Examples |
| 148 | + |
| 149 | +### Python Client |
| 150 | +```python |
| 151 | +import requests |
| 152 | + |
| 153 | +# Create draft |
| 154 | +response = requests.post("http://localhost:8000/drafts/create-markup", json={ |
| 155 | + "title": "My Post", |
| 156 | + "markup_content": "Title:: Hello | Text:: This is **bold** | Subscribe:: Join" |
| 157 | +}) |
| 158 | + |
| 159 | +draft = response.json() |
| 160 | +print(f"Created draft {draft['draft_id']}") |
| 161 | + |
| 162 | +# Publish draft |
| 163 | +requests.post(f"http://localhost:8000/drafts/{draft['draft_id']}/publish", json={ |
| 164 | + "send_email": True, |
| 165 | + "audience": "everyone" |
| 166 | +}) |
| 167 | +``` |
| 168 | + |
| 169 | +### JavaScript/Node.js |
| 170 | +```javascript |
| 171 | +// Create draft |
| 172 | +const response = await fetch('http://localhost:8000/drafts/create-markup', { |
| 173 | + method: 'POST', |
| 174 | + headers: { 'Content-Type': 'application/json' }, |
| 175 | + body: JSON.stringify({ |
| 176 | + title: 'My Post', |
| 177 | + markup_content: 'Title:: Hello | Text:: This is **bold** | Subscribe:: Join' |
| 178 | + }) |
| 179 | +}); |
| 180 | + |
| 181 | +const draft = await response.json(); |
| 182 | +console.log(`Created draft ${draft.draft_id}`); |
| 183 | +``` |
| 184 | + |
| 185 | +### cURL Examples |
| 186 | +```bash |
| 187 | +# Create draft |
| 188 | +curl -X POST "http://localhost:8000/drafts/create-markup" \ |
| 189 | + -H "Content-Type: application/json" \ |
| 190 | + -d '{"title": "Test", "markup_content": "Text:: Hello **world**"}' |
| 191 | + |
| 192 | +# List drafts |
| 193 | +curl "http://localhost:8000/drafts" |
| 194 | + |
| 195 | +# Publish draft |
| 196 | +curl -X POST "http://localhost:8000/drafts/123456/publish" \ |
| 197 | + -H "Content-Type: application/json" \ |
| 198 | + -d '{"send_email": true, "audience": "everyone"}' |
| 199 | +``` |
| 200 | + |
| 201 | +## Error Handling |
| 202 | + |
| 203 | +API returns standard HTTP status codes: |
| 204 | +- `200` - Success |
| 205 | +- `400` - Bad request (invalid markup syntax) |
| 206 | +- `500` - Internal server error (environment not configured, API failure) |
| 207 | + |
| 208 | +Example error response: |
| 209 | +```json |
| 210 | +{ |
| 211 | + "detail": "Invalid markup syntax: unexpected token at position 15" |
| 212 | +} |
| 213 | +``` |
| 214 | + |
| 215 | +## Development & Testing |
| 216 | + |
| 217 | +### Interactive Documentation |
| 218 | +Visit http://localhost:8000/docs for Swagger UI with: |
| 219 | +- Interactive API testing |
| 220 | +- Request/response schemas |
| 221 | +- Example requests |
| 222 | + |
| 223 | +### Health Check |
| 224 | +```bash |
| 225 | +curl http://localhost:8000/ |
| 226 | +``` |
| 227 | + |
| 228 | +Returns API info and available endpoints. |
| 229 | + |
| 230 | +## Production Deployment |
| 231 | + |
| 232 | +For production use: |
| 233 | + |
| 234 | +1. **Environment Variables**: Set via environment instead of `.env` file |
| 235 | +2. **Authentication**: Add API key authentication |
| 236 | +3. **Rate Limiting**: Implement rate limits |
| 237 | +4. **HTTPS**: Use reverse proxy (nginx) with SSL |
| 238 | +5. **Process Management**: Use systemd or Docker |
| 239 | + |
| 240 | +Example Docker deployment: |
| 241 | +```dockerfile |
| 242 | +FROM python:3.9-slim |
| 243 | +COPY . /app |
| 244 | +WORKDIR /app |
| 245 | +RUN pip install -r requirements.txt |
| 246 | +EXPOSE 8000 |
| 247 | +CMD ["python", "api_server.py"] |
| 248 | +``` |
| 249 | + |
| 250 | +## Advantages over CLI |
| 251 | + |
| 252 | +- ✅ **HTTP Integration**: Easy to call from any programming language |
| 253 | +- ✅ **Web Interface**: Interactive documentation at `/docs` |
| 254 | +- ✅ **JSON Responses**: Structured data instead of console output |
| 255 | +- ✅ **Scalable**: Can handle multiple requests |
| 256 | +- ✅ **Remote Access**: Can run on server, access remotely |
| 257 | +- ✅ **No File Dependencies**: Markup passed directly in requests |
| 258 | + |
| 259 | +Perfect for: |
| 260 | +- Web applications |
| 261 | +- Automation scripts |
| 262 | +- CI/CD pipelines |
| 263 | +- Third-party integrations |
| 264 | +- Mobile app backends |
0 commit comments