URL Shortener API with Minimal APIs
Build a URL shortener service with ASP.NET Core Minimal APIs and Entity Framework Core. You send it a long link and get back a short one, like http://localhost:5000/aB3xK9; anyone opening the short link is redirected to the original, and every click is counted. An analytics endpoint shows how many clicks a link got and when it was created, and the create endpoint is rate limited so one user can't flood it. Links are stored in SQLite. You'll learn Minimal API routing and results, EF Core with SQLite and migrations, generating short codes that don't collide, validating URLs, redirects, and ASP.NET Core's built-in rate limiting.
- Estimate
- ~9.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
ASP.NET Core Minimal APIs · Entity Framework Core · SQLite
Project roadmap
- 01
Create the project and model
~1.5hCreate a project with dotnet new web and add EF Core with the SQLite provider. Define a ShortLink entity (Id, Code with a unique index, OriginalUrl, CreatedAt, ClickCount) and an AppDbContext. Create and apply the first migration. Done when: the database file is created with the ShortLinks table.
- 02
Create short links
~2hAdd POST /api/links accepting { "url": "..." }. Check the URL is absolute http or https with Uri.TryCreate and at most 2,048 characters. Generate a random 7-character code from letters and digits using RandomNumberGenerator, and try again when the code already exists. Return 201 with the code and the full short URL. Done when: posting a valid URL returns a short link, and an invalid one returns 400.
- 03
Redirect and count clicks
~1.5hAdd GET /{code}: look up the link, increase ClickCount with a single UPDATE (ExecuteUpdateAsync) so simultaneous clicks aren't lost, and return a 302 redirect to the original URL. Return 404 for unknown codes. Done when: opening a short link in the browser lands on the original page, and the click count increases.
- 04
Add analytics
~1hAdd GET /api/links/{code}/stats returning the original URL, the creation date and the click count. Use Minimal API route groups (MapGroup) to organize the /api endpoints, and TypedResults for the responses. Done when: the stats endpoint shows the correct click count after several visits.
- 05
Rate limit link creation
~1.5hAdd ASP.NET Core rate limiting with AddRateLimiter: a fixed-window policy partitioned by the client's IP address, allowing for example 10 new links per minute, applied only to the POST endpoint with RequireRateLimiting. Return 429 with a Retry-After header when the limit is exceeded. Done when: the 11th request within a minute gets 429, and redirects are never limited.
- 06
Write integration tests
~2hCreate an xUnit project using WebApplicationFactory with a SQLite in-memory database. Test creating a link, the redirect (disable automatic redirects in the test client to check the 302 and Location header), click counting, stats, unknown codes, invalid URLs and rate limiting. Done when: dotnet test passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack