A TypeScript-based Edge Function API that analyzes website performance using Google PageSpeed Insights and generates Azion-specific optimization recommendations. Built exclusively for Azion Edge Functions with a modern, streamlined architecture.
- Performance Analysis: Complete website performance analysis using PageSpeed Insights API
- Azion Solutions Mapping: Intelligent mapping of performance issues to specific Azion products and solutions
- CrUX Data Integration: Chrome User Experience Report data for real user metrics
- Multiple Output Formats: JSON data, HTML reports, and structured solutions
- Interactive Manual Interface: Real-time testing interface with progress tracking and detailed logging
- Edge Function Native: Optimized for Azion's global edge network
- TypeScript: Full type safety and modern development experience
Returns detailed performance analysis in JSON format (analysis data only, no HTML report).
Generates and returns an HTML performance report.
Returns complete analysis with both JSON data and HTML report included.
Returns structured Azion solutions and optimization insights in JSON format optimized for LLMs and tools.
Health check endpoint returning service status.
Interactive manual testing interface with real-time progress tracking.
API documentation endpoint.
All analysis endpoints accept the same request body:
{
"url": "https://example.com",
"device": "mobile",
"use_crux": false,
"weeks": 25,
"follow_redirects": false
}Parameters:
- url (required): The website URL to analyze
- device (optional): Device type -
mobile,desktop, ortablet(default:mobile) - use_crux (optional): Include Chrome User Experience Report data (default:
false) - weeks (optional): Number of weeks of CrUX data to fetch (default:
25) - follow_redirects (optional): Automatically follow HTTP redirects before analysis (default:
false)
- Node.js 18+
- npm or yarn
- Google PageSpeed Insights API key
- Azion CLI
-
Clone the repository
git clone <repository-url> cd pagespeedinsights-api
-
Install dependencies
npm install
-
Configure environment variables
For development, create a
.envfile:cp .env.example .env # Edit .env and add your PageSpeed Insights API key -
Start the development server
npm run dev
The API will be available at
http://localhost:3333
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the PageSpeed Insights API
- Create credentials (API key)
- For development: Add the API key to your
.envfile - For production: Configure as an Azion Platform Environment Variable
-
Install Azion CLI
npm install -g azion
-
Login to Azion
azion login
-
Configure API Key for Production
Set up the API key as an Azion Platform Environment Variable:
- Go to your Azion Console
- Navigate to Edge Functions
- Select your function
- Add environment variable:
PAGESPEED_INSIGHTS_API_KEY
-
Deploy to Azion
npm run deploy
src/
βββ function/
β βββ index.ts # Azion Edge Function entry point
βββ services/
β βββ analyzer.ts # Core analysis service
β βββ azion-solutions.ts # Azion solutions mapping
β βββ crux.ts # Chrome UX Report service
β βββ pagespeed.ts # PageSpeed Insights API client
β βββ report.ts # HTML report generation
βββ types/
β βββ event.ts # Edge function types
β βββ global.d.ts # Global type definitions
β βββ index.ts # Core type definitions
βββ utils/
βββ error-handling.ts # Unified error handling
βββ manual-interface-styles.ts # CSS styles for manual interface
βββ manual-interface-template.ts # HTML template for manual interface
βββ validation.ts # Request validation utilities
Basic Analysis:
curl -X POST https://your-domain.com/analyze \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'Desktop Analysis with CrUX Data:
curl -X POST https://your-domain.com/analyze \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com",
"device": "desktop",
"use_crux": true,
"follow_redirects": true
}'Get Azion Solutions:
curl -X POST https://your-domain.com/solutions \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}'Generate HTML Report:
curl -X POST https://your-domain.com/report \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com"}' \
-o report.htmlconst response = await fetch('https://your-domain.com/analyze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://example.com',
device: 'mobile',
use_crux: true,
follow_redirects: true
})
});
const analysis = await response.json();
console.log(analysis);{
"url": "https://example.com",
"final_url": "https://example.com",
"device": "mobile",
"timestamp": "2024-01-01T00:00:00.000Z",
"analysis": {
"performance": {
"score": 85,
"issues": [...]
},
"accessibility": {
"score": 92,
"issues": [...]
},
"best_practices": {
"score": 88,
"issues": [...]
},
"seo": {
"score": 95,
"issues": [...]
}
},
"azion_recommendations": {
"solution_count": 5,
"marketing_data": {...}
},
"crux_data": {...},
"html_report": "..." // Only included in /full endpoint
}The /solutions endpoint returns structured data optimized for LLMs and tools:
{
"metadata": {
"url": "https://example.com",
"overall_performance_score": 85,
"total_issues_detected": 12,
"api_version": "3.0.0"
},
"performance_assessment": {
"scores_by_category": {...},
"issues_breakdown": {
"high_priority": {...},
"medium_priority": {...},
"low_priority": {...}
}
},
"azion_solutions": {
"summary": {...},
"recommended_products": [...],
"implementation_roadmap": [...]
},
"optimization_insights": {
"quick_wins": [...],
"major_improvements": [...],
"console_errors": {...}
},
"next_steps": {...}
}- Global Distribution: Deployed to Azion's edge network for low latency
- Serverless: No server management required
- Auto-scaling: Handles traffic spikes automatically
- Cost-effective: Pay only for actual usage
- Edge Function Native: Optimized exclusively for Azion Edge Functions
- Simplified Dependencies: Minimal footprint with only essential packages
- Better Performance: Native edge execution without Express.js overhead
- Easier Maintenance: Single deployment target and architecture
- Eliminated 1000+ lines of duplicated code
- Single source of truth for manual interface, validation, and error handling
- Unified user experience with shared utilities in
src/utils/ - Modular architecture supporting future enhancements
# Development
npm run dev # Start Azion Edge Functions locally (port 3333)
# Building
npm run build # Build for Azion Edge Functions
# Deployment
npm run deploy # Deploy to Azion Edge Network
# Utilities
npm run clean # Remove build artifacts (.edge/ directory)
npm test # Run tests (placeholder)PAGESPEED_INSIGHTS_API_KEY=your-api-key-hereConfigure the API key as an Azion Platform Environment Variable:
- Go to your Azion Console
- Navigate to Edge Functions
- Select your function
- Add environment variable:
PAGESPEED_INSIGHTS_API_KEY=your-api-key-here
The API uses standardized error responses:
{
"error": {
"code": "VALIDATION_FAILED",
"message": "Invalid request parameters",
"details": "URL is required",
"timestamp": "2024-01-01T00:00:00.000Z"
}
}Common Error Codes:
VALIDATION_FAILED: Invalid request parametersAPI_KEY_MISSING: PageSpeed Insights API key not configuredPAGESPEED_API_ERROR: Error from PageSpeed Insights APIANALYSIS_FAILED: General analysis failureREPORT_GENERATION_FAILED: HTML report generation error
The /manual endpoint provides an interactive testing interface with:
- Real-time Progress Tracking: Dynamic progress bars with step-by-step updates
- Collapsible Log View: Color-coded log entries (INFO, SUCCESS, WARNING, ERROR)
- Elapsed Time Display: Analysis duration tracking
- Multiple Output Types: Switch between JSON analysis, HTML reports, and Azion solutions
- Follow Redirects Option: Test redirect handling functionality
- Download & Copy Actions: Easy result sharing and saving
Visit the /docs endpoint for complete API documentation, or use the interactive manual interface at /manual for hands-on testing.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests (when test suite is implemented)
- Submit a pull request
MIT License - see LICENSE file for details.
For issues and questions:
- Check the GitHub Issues
- Review the API documentation at
/docsendpoint - Use the manual interface at
/manualfor testing - Consult the Azion documentation for Edge Functions