A complete URL shortener application built with SvelteKit and PocketBase. This example demonstrates how to build a full-stack web application with URL shortening functionality, including API endpoints for creating short URLs and redirecting to original URLs.
/api/shorten?url=<long_url>
/{short_code}
to redirect to the original URLnpm install
# or
pnpm install
Download and extract PocketBase from pocketbase.io
Create a new PocketBase project:
./pocketbase serve
Open the PocketBase admin panel (usually at http://localhost:8090/_/
)
Import the database schema from .pocketbase-export.json
:
.pocketbase-export.json
file from this repositoryCreate a .env
file in the project root:
POCKETBASE_URL=http://127.0.0.1:8090
Make sure PocketBase is running on the specified URL.
npm run dev
# or
pnpm dev
Visit http://localhost:5173
to see the application.
POST /api/shorten?url=<encoded_url>
Creates a new short URL for the provided long URL.
Example:
curl "http://localhost:5173/api/shorten?url=https%3A%2F%2Fexample.com"
Response:
{
"status": 200,
"message": "url shortened successfully",
"shortcode": "abc123"
}
GET /{short_code}
Redirects to the original URL and increments the hit counter.
Example:
GET http://localhost:5173/abc123
→ Redirects to https://example.com
The application uses a urls
collection in PocketBase with the following fields:
id
(text): Auto-generated PocketBase IDshort_code
(text): 6-character alphanumeric codelong_url
(text): The original URLhits
(number): Number of times the short URL has been accessedcreated
(date): Auto-generated creation timestampnpm run dev
- Start development servernpm run build
- Build for productionnpm run preview
- Preview production buildnpm run check
- Type checking and lintingnpm run format
- Format code with Prettiersrc/
├── lib/
│ ├── index.ts # Utility functions and types
│ └── pb.server.ts # PocketBase client configuration
├── routes/
│ ├── +page.svelte # Main page (can be customized)
│ ├── [short_code]/
│ │ └── +server.ts # URL redirect handler
│ └── api/
│ └── shorten/
│ └── +server.ts # URL shortening API
Build the application:
npm run build
Deploy the build/
directory to your hosting provider
Set the POCKETBASE_URL
environment variable to point to your PocketBase instance
For production deployment, consider using SvelteKit adapters for your target platform (Vercel, Netlify, etc.).
This is an example project demonstrating SvelteKit + PocketBase integration. Feel free to modify and extend it for your own use cases!