queryParamsREADME.md1 match
1# Handling query params in requests
23Using the standard [URL#searchParams](https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams) method, you can grab query parameters out of any val that is operating using the [Web API](https://docs.val.town/api/web).
45This val demonstrates how to grab one or more query parameters. It returns the all the query parameters found as a json response.
htmlExampleREADME.md2 matches
1# Returning HTML from the Val Town Web API
23This just lets you use the standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with our [Web API](https://docs.val.town/api/web) to return an HTML response from this Val.
gracefulVioletTunavt3 matches
1export default async function createProject(projectName) {
2const apiToken = Deno.env.get("VAL_TOWN_RW_PROJECTS");
34const response = await fetch("https://api.val.town/v1/projects", {
5method: "POST",
6headers: {
7"Content-Type": "application/json",
8"Authorization": `Bearer ${apiToken}`,
9},
10body: JSON.stringify({
891. Click `Fork`
102. Change `location` (Line 4) to describe your location. It accepts fairly flexible English descriptions which it turns into locations via [nominatim's geocoder API](https://www.val.town/v/stevekrouse/nominatimSearch).
113. Click `Run`
12
9const getForecast = async () => {
10const forecastData = await fetchJSON(
11`http://dataservice.accuweather.com/forecasts/v1/daily/1day/${accuweatherCityCode}?apikey=${process.env.accuWeather}&details=true&metric=true`,
12);
13return forecastData.DailyForecasts[0];
projectConverterDraftconverter.ts15 matches
3}
45async function convertValToProject(input: string, apiToken: string): Promise<string> {
6try {
7const projectName = generateProjectName();
89// Create a new project
10const projectResponse = await fetch("https://api.val.town/v1/projects", {
11method: "POST",
12headers: {
13"Content-Type": "application/json",
14"Authorization": `Bearer ${apiToken}`,
15},
16body: JSON.stringify({
37const directories = getUniqueDirectories(files);
38for (const dir of directories) {
39await createDirectory(project.id, dir, apiToken);
40}
4142// Create all files
43for (const file of files) {
44await createFile(project.id, file, apiToken);
45}
46138}
139140async function createDirectory(projectId: string, directoryPath: string, apiToken: string): Promise<void> {
141const encodedPath = encodeURIComponent(directoryPath);
142const response = await fetch(`https://api.val.town/v1/projects/${projectId}/files/${encodedPath}`, {
143method: "POST",
144headers: {
145"Content-Type": "application/json",
146"Authorization": `Bearer ${apiToken}`,
147},
148body: JSON.stringify({ type: "directory" }),
158projectId: string,
159file: { path: string; content: string; type: string },
160apiToken: string,
161): Promise<void> {
162const validTypes = ["script", "file", "directory", "http"];
167168const encodedPath = encodeURIComponent(file.path);
169const fileResponse = await fetch(`https://api.val.town/v1/projects/${projectId}/files/${encodedPath}`, {
170method: "POST",
171headers: {
172"Content-Type": "application/json",
173"Authorization": `Bearer ${apiToken}`,
174},
175body: JSON.stringify({
443app.post("/convert", async (c) => {
444try {
445const { valCode, apiToken } = await c.req.json();
446
447if (!apiToken) {
448return c.json({ message: "Error: API token is required" }, 400);
449}
450
451const result = await convertValToProject(valCode, apiToken);
452return c.json({ message: result });
453} catch (error) {
projectConverterDraftindex.ts4 matches
31app.post("/convert", async (c) => {
32try {
33const { valCode, apiToken } = await c.req.json();
3435if (!apiToken) {
36return c.json({ message: "Error: API token is required" }, 400);
37}
3840const { convertValToProject } = await import("./converter.ts");
4142const result = await convertValToProject(valCode, apiToken);
43return c.json({ message: result });
44} catch (error) {
5## Files
67* `index.ts` - The main HTTP entrypoint for the project that serves frontend assets and API endpoints
89## Important Notes
projectConverterDraftApp.tsx11 matches
5function App() {
6const [valCode, setValCode] = useState("");
7const [apiToken, setApiToken] = useState("");
8const [result, setResult] = useState("");
9const [isLoading, setIsLoading] = useState(false);
12e.preventDefault();
1314if (!apiToken.trim()) {
15setResult("Please enter your Val Town API token");
16return;
17}
27method: "POST",
28headers: { "Content-Type": "application/json" },
29body: JSON.stringify({ valCode, apiToken }),
30});
3152<form onSubmit={handleSubmit}>
53<div style={{ marginBottom: "20px" }}>
54<label htmlFor="apiToken">Val Town API Token:</label>
55<input
56id="apiToken"
57type="password"
58className="api-token"
59value={apiToken}
60onChange={(e) => setApiToken(e.target.value)}
61placeholder="Enter your Val Town API token"
62/>
63<small style={{ display: "block", marginTop: "5px", color: "#666" }}>
64You can generate an API token in your Val Town settings
65</small>
66</div>
74}
7576.api-token {
77width: 100%;
78padding: 10px;