This has been depreciated. Please use url-shortening-api-netlify-edge-superbase instead.
url-shortening-api-netlify-supabase is a URL shortener service. It is a serverless application that provides URL shortening and retrieval functionalities. Utilizing Netlify and Supabase, a cloud-based database, the application offers an efficient and scalable solution for creating short URLs that redirect to the original, longer URLs.
- 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.
- Automatic URL Deduplication: Checks the database to avoid storing duplicate entries for the same URL.
- High Performance: Leveraging the cloud database's capabilities for high-speed data retrieval and storage.
- Error Handling: Robust error handling mechanisms to provide clear feedback on the nature of issues encountered.
- Node.js: The runtime environment for the serverless function.
- Supabase: A cloud-hosted database that stores URL mappings.
- Dotenv: A module for loading environment variables from a
.env
file. - Nanoid: Generates short, unique identifiers for URLs.
- Validator: Validates URLs to ensure they include the required protocol.
- Querystring: For parsing the body of POST requests.
Before installing, ensure you have Node.js and npm (Node Package Manager) installed on your system. You can download and install Node.js from Node.js official website.
To install and use url-shortening-api-netlify-supabase, follow these steps:
- Clone the Repository: Begin by cloning the repository containing the url-shortening-api-netlify-supabase to your local machine.
git clone https://github.com/samestrin/url-shortening-api-netlify-supabase/
- Install Dependencies:
npm install dotenv @supabase/supabase-js shortid validator querystring
- Install Development Dependencies:
npm install --save-dev netlify-lambda
- Build Lambda: Build your serverless functions using the
netlify-lambda
package. This will include all the Node.js dependencies.
npm run build:lambda
- Setup Your Supabase Database: Create a new project with a
urls
table for storage. Detailed directions are available here.
-
Deploy: Deploy to Netlify.
-
Configure Environment Variables: Create a
.env
file in the project root and defineSUPABASE_URL
andSUPABASE_ANON_KEY
with your Supabase project credentials. Optionally setURL_BASE
to your hostname (example: https://frwrd.ing/). -
Test: Visit /count to see the number of URLs stored in your database.
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
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.