---
# System prepended metadata

title: Untitled

---

ChatGPT prompt for Mochi: "I need a serverless function that takes url params and applies a discount code to the price of tickets on a page in a React Typescript based AstroJS project .  It will be hosted on Netlify."

``` JavaScript
// ./netlify/functions/apply_discount.ts

import { Handler } from '@netlify/functions';

const handler: Handler = async (event, context) => {
  const params = event.queryStringParameters;

  const discountCode = params.discount;
  let ticketPrice = Number(params.price);

  // Apply the discount code
  if (discountCode === 'DISCOUNT10') {
    ticketPrice *= 0.9;
  } else if (discountCode === 'DISCOUNT20') {
    ticketPrice *= 0.8;
  }

  // Returns the new ticket price
  return {
    statusCode: 200,
    body: JSON.stringify({ ticketPrice }),
  };
};

export { handler };
```
``` JavaScript
// ./src/components/TicketPrice.jsx

import { useEffect, useState } from 'react';

const TicketPrice = () => {
  const [price, setPrice] = useState(null);

  useEffect(() => {
    const fetchPrice = async () => {
      const response = await fetch('/.netlify/functions/apply_discount?discount=DISCOUNT10&price=100');
      const data = await response.json();
      setPrice(data.ticketPrice);
    };

    fetchPrice();
  }, []);

  if (price === null) return <div>Loading...</div>;

  return <div>The discounted price is: {price}</div>;
};
```