url-shortening-api-netlify-edge-supabase is a URL shortener service. It is a serverless application that provides URL shortening and retrieval functionalities. Utilizing Netlify Edge Functions and Supabase, a cloud-based database, the application offers a high performance, efficient and scalable solution for creating short URLs that redirect to the original, longer URLs.
Netlify Edge Functions are run at the network edge, closer to your users, translating into faster load times. Netlify also offers 1M/month Edge Function executions per month at the free tier, a significant increase over 125k/month Function executions allocated at the same tier. At the Pro level you have 2m/month to work with.
- Decreased latency: Running logic closer to the end user can reduce not only the volume of data, but the distance that data needs to travel. If a user were sending a request in Arizona, a response time from a local node would be lower than one in London.
- Reduction in cold start boot times: For serverless functions, cold start boot times average anywhere from 50-500ms on average. The cold start average for edge functions, on the other hand, is drastically reduced (50-200ms).
This replaces the legacy url-shortening-api-netlify-supabase project.
- URL Shortening: Convert long URLs into short, manageable links that are easier to share.
- URL Validation: Ensures that only valid URLs with proper protocols are processed.
- URL Redirection: Redirect users to the original long URL based on the short URL.
- Link Tracking: Track which short URLs are served.
- Retrieve Latest Shortened URLs: Access the most recently created short URLs.
- URL Count: Get the total number of URLs shortened.
- API Versioning: Retrieve the current version of the API.
- CORS Support: Handle cross-origin resource sharing with appropriate headers.
- Error Handling: Graceful error handling with appropriate HTTP status codes and error messages.
- Node.js: The script runs in a Node.js environment.
- Deno: A secure runtime for JavaScript and TypeScript.
- Supabase (REST API): A backend-as-a-service providing a Postgres database, authentication, storage, and more.
- Netlify Edge Functions: Serverless functions that run at the edge, closer to your users.
- multiParser: Parses multipart form data.
To set up the project locally, follow these steps:
- Clone the Repository:
git clone https://github.com/samestrin/url-shortening-api-netlify-edge-supabase.git
cd url-shortening-api-netlify-edge-supabase
- Install Dependencies: Ensure you have the required dependencies installed. Use npm or yarn to install any necessary packages.
npm install
- Set Up Netlify CLI: Install the Netlify CLI to deploy and test the functions locally.
npm install -g netlify-cli
-
Configure Environment Variables: Create a
.env
file in the project root and defineSUPABASE_URL
andSUPABASE_ANON_KEY
with your Supabase project credentials. Optionally setURLSHORT_URL_BASE
to your hostname (example: https://frwrd.ing/), andURLSHORT_TRACK_CLICKS
totrue
to enable short link click tracking. -
Run the Functions Locally: Use the Netlify CLI to run the edge functions locally.
netlify dev
The netlify.toml
file contains the configuration for the edge functions. Each function is mapped to a specific endpoint:
[build]
publish = "public"
[build.environment]
NODE_VERSION = "20"
[[edge_functions]]
path = "/shorten"
function = "shorten"
[[edge_functions]]
path = "/count"
function = "count"
[[edge_functions]]
path = "/latest"
function = "latest"
[[edge_functions]]
path = "/version"
function = "version"
[[edge_functions]]
path = "/*"
function = "redirect"
Endpoint: /shorten
Method: POST
Shorten a long URL and return the shortened URL.
url
: The URL to be shortened.
Use a tool like Postman or curl to make a request:
curl -X POST \
https://localhost/shorten \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'url=https://www.google.com'
The server responds with:
{"shortUrl":"lqywv6P"}
Endpoint: /[shortId]
Method: GET
Based on shortened URL, e.g. /lqywv6P
HTTP 301 forward to a long url.
This endpoint is accessed by navigating directly to the shortened URL.
Use curl to make a request:
curl http://localhost/[shortId]
Endpoint: /latest
Method: GET
Retrieve the latest URLs shortened.
This endpoint is accessed by navigating directly to /latest.
Use curl to make a request:
curl http://localhost/latest
Endpoint: /count
Method: GET
Retrieve the number of URLs shortened.
This endpoint is accessed by navigating directly to /count.
Use curl to make a request:
curl http://localhost/count
Endpoint: /version
Method: GET
Retrieve the current version of the API.
This endpoint is accessed by navigating directly to /version.
Use curl to make a request:
curl http://localhost/version
The server responds with:
{
"name": "url-shortening-api-netlify-edge-supabase",
"version": "0.0.1",
"description": "url-shortening-api-netlify-edge-supabase is a URL shortener service using Netlify Edge Functions and the Supabase REST API.",
"author": "Sam Estrin",
"homepage": "https://github.com/samestrin/url-shortening-api-netlify-edge-supabase#readme"
}
The server responds with appropriate CORS headers such as Access-Control-Allow-Origin.
The API handles errors gracefully and returns appropriate error responses:
- 400 Bad Request: Invalid request parameters.
- 404 Not Found: Resource not found.
- 405 Method Not Allowed: Invalid request method (not GET or POST).
- 500 Internal Server Error: Unexpected server error.
Contributions to this project are welcome. Please fork the repository and submit a pull request with your changes or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.