200 const url = new URL(req.url);
201
202 // Handle the expand API endpoint
203 if (url.pathname === "/expand") {
204 if (req.method !== "POST") {
3import { hc } from "https://esm.sh/hono@4.7.7/client?deps=hono@4.7.7";
4import { QueryClient } from "https://esm.sh/@tanstack/react-query@5.74.7?deps=react@19.0.0";
5import { AppType } from "../backend/api/mod.ts";
6import { APP_URL } from "../shared/consts.ts";
7
8export const client = hc<AppType>(
9 typeof window !== "undefined"
10 ? window.location.origin + "/api"
11 : APP_URL + "/api",
12 { init: { credentials: "include" } },
13);
7- Expands even single-word prompts into rich, detailed content
8- Simple, clean user interface with example suggestions
9- Real-time prompt expansion using OpenAI's API
10- Responsive design that works on mobile and desktop
11- Error handling and loading states
46## Notes
47
48This application uses the OpenAI API which requires an API key. Make sure your Val Town account has an OpenAI API key configured in the environment variables.
2
3export default async function (req: Request): Promise<Response> {
4 // Handle API requests for prompt expansion
5 if (req.url.includes("/api/expand") && req.method === "POST") {
6 try {
7 const { prompt } = await req.json();
199
200 try {
201 const response = await fetch('/api/expand', {
202 method: 'POST',
203 headers: {
37
38- Built as an HTTP-triggered Val in Val Town
39- Uses OpenAI's API to generate prompts
40- Frontend styled with TailwindCSS
41- Handles both GET and POST requests in a single endpoint
42- Includes error handling for API failures
43
44## Setup
46To use this application, you need to:
47
481. Set up an OpenAI API key as an environment variable in Val Town
492. Deploy the Val with HTTP trigger enabled
50
51## Environment Variables
52
53- `OPENAI_API_KEY`: Your OpenAI API key for generating prompts
54
55## Customization
7
8export default async function(req: Request): Promise<Response> {
9 // Handle API requests for promo code generation
10 if (req.url.includes('/api/generate-promo')) {
11 return handlePromoGeneration(req);
12 }
21
22/**
23 * Handles the API request to generate a promo code
24 */
25async function handlePromoGeneration(req: Request): Promise<Response> {
214 generateBtn.textContent = 'Generating...';
215
216 // Call the API to generate promo code
217 const response = await fetch('/api/generate-promo', {
218 method: 'POST',
219 headers: {
2
3export const env = safeEnv({
4 GOOGLE_GENERATIVE_AI_API_KEY: string(),
5});
6
3const PH_HOST = Deno.env.get("PH_HOST")!;
4const PH_PROJECT_ID = Deno.env.get("PH_PROJECT_ID")!;
5const PH_KEY = Deno.env.get("PH_PERSONAL_API_KEY")!;
6
7const headers = {
43
44 const res = await fetch(
45 `${PH_HOST}/api/projects/${PH_PROJECT_ID}/query/`, // Queries endpoint
46 { method: "POST", headers, body: JSON.stringify(body) },
47 );
9import { thinkTool } from "./tools/think.tsx";
10
11const apiKey = Deno.env.get("ANTHROPIC_API_KEY");
12
13const anthropic = createAnthropic({
14 apiKey,
15});
16
17 console.log("Message:", decodeURIComponent(message));
18
19 // Make the OpenAI API call
20 const completion = await openai.chat.completions.create({
21 messages: [