# Smolr Developer API

Base URL: `https://smolr.pages.dev`

Smolr exposes a REST API for programmatic link creation and retrieval. API access is available from the Developer API section of the signed-in dashboard. Create a key there and store it securely; the full key is shown only once.

## Authentication

Send your API key using the Bearer scheme:

```http
Authorization: Bearer smr_YOUR_API_KEY
Content-Type: application/json
```

Never expose an API key in browser-side JavaScript, public repositories, or mobile-app source code. Use a trusted server to make authenticated API requests.

## Endpoints

### Health

`GET /api/health`

Returns service status. No authentication required.

### List links

`GET /api/v1/links`

Returns up to 100 links belonging to the API key owner.

### Create a link

`POST /api/v1/links`

Request:

```json
{
  "destination_url": "https://example.com/a-long-campaign-url",
  "slug": "summer-sale",
  "title": "Summer sale"
}
```

Only `destination_url` is required. Custom aliases must be 3–40 characters using letters, numbers, hyphens or underscores. Destination URLs must use HTTP or HTTPS.

Successful responses include the link record and `short_url` in the format `https://smolr.pages.dev/s/{slug}`.

## Browser/session endpoints

These endpoints are used by the Smolr dashboard and require a signed-in Google session:

- `POST /api/auth/google` — exchange a Google Identity Services credential for a Smolr session.
- `POST /api/auth/logout` — clear the session cookie.
- `GET /api/me` — current profile.
- `GET|POST /api/links` — list and create links.
- `PATCH|DELETE /api/links/:id` — update or delete an owned link.
- `GET|POST /api/qr` — list and save QR records.
- `GET|POST /api/keys` — list and create API keys.
- `DELETE /api/keys/:id` — revoke an API key.
- `GET /api/analytics/summary` — account link and click totals.

## Errors

Errors return JSON in the form:

```json
{ "error": "A short explanation" }
```

Common status codes: 400 invalid input, 401 missing/invalid authentication, 404 unknown resource, 503 missing server configuration.

## Example: create a link

```bash
curl -X POST "https://smolr.pages.dev/api/v1/links" \
  -H "Authorization: Bearer smr_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"destination_url":"https://example.com/article","title":"Article"}'
```

## Current limitations

- API v1 currently supports link listing and creation.
- Analytics are basic click counts; richer breakdowns are planned.
- API rate limits, granular scopes, usage quotas and key rotation are not implemented yet.
- Google OAuth requires a Google Cloud OAuth Web client ID configured in Cloudflare Pages.
