Examples

This section provides practical examples of using Buzzy Functions and Constants together to build powerful integrations and automations.

Weather API Integration

This example shows how to create a weather function that fetches data from an external API and displays it in your Buzzy app.

Constants Setup

// Create these constants in your app
WEATHER_API_KEY (secret) = "your-openweathermap-api-key"
WEATHER_API_URL = "https://api.openweathermap.org/data/2.5"
DEFAULT_UNITS = "metric"

Function Code

export const main = async (event) => {
  console.log('Weather function called with:', event);
  
  try {
    const { city, units } = event.body;
    
    if (!city) {
      return {
        statusCode: 400,
        body: { error: 'City parameter is required' }
      };
    }
    
    const apiKey = process.env.WEATHER_API_KEY;
    const baseUrl = process.env.WEATHER_API_URL;
    const weatherUnits = units || process.env.DEFAULT_UNITS;
    
    const response = await axios.get(`${baseUrl}/weather`, {
      params: {
        q: city,
        appid: apiKey,
        units: weatherUnits
      }
    });
    
    const weather = response.data;
    
    return {
      statusCode: 200,
      body: {
        success: true,
        city: weather.name,
        country: weather.sys.country,
        temperature: weather.main.temp,
        description: weather.weather[0].description,
        humidity: weather.main.humidity,
        windSpeed: weather.wind.speed,
        timestamp: new Date().toISOString()
      }
    };
  } catch (error) {
    console.error('Weather API error:', error);
    return {
      statusCode: 500,
      body: {
        success: false,
        error: error.message
      }
    };
  }
};

Function Environment Variables

Test Payload

AI Chat Integration

Create an AI-powered chat function that integrates with OpenAI's GPT API and saves conversations to your Buzzy database.

Constants Setup

Function Code

Function Environment Variables

Stripe Webhook Handler

Handle Stripe payment webhooks and update your Buzzy application with payment status.

Constants Setup

Function Code

Function Configuration

  • Authentication Type: Specific Domains

  • Allowed Domains: ['https://hooks.stripe.com']

Data Synchronization

Sync data between your Buzzy app and an external CRM system.

Constants Setup

Function Code

Email Notification System

Send automated emails using an external email service when certain conditions are met.

Constants Setup

Function Code

Best Practices from Examples

Security

  • Always use Constants for API keys and sensitive data

  • Verify webhook signatures for external integrations

  • Implement proper error handling and logging

  • Use environment variables instead of hardcoded values

Error Handling

  • Return appropriate HTTP status codes

  • Include descriptive error messages

  • Log errors for debugging

  • Implement graceful fallbacks where possible

Performance

  • Use async/await for better performance

  • Implement timeouts for external API calls

  • Cache frequently accessed data when appropriate

  • Monitor function execution time

Integration Patterns

  • Authenticate with Buzzy using the buzzy-api-nodejs client

  • Use consistent data structures for API responses

  • Implement proper data validation

  • Log important events for audit trails

Testing

  • Create comprehensive test payloads

  • Test error scenarios and edge cases

  • Verify external API integrations work correctly

  • Monitor function logs during testing

Last updated