# Embed Widgets

The `/embed/location` endpoint provides a ready-made UI widget that displays precipitation and weather data for a specific location. Embed it in any web application using an `<iframe>`.

## Live Examples

- **[Simple HTML Embed](https://stackblitz.com/edit/iframe-embed-example?file=index.html)** — Basic iframe implementation
- **[React Component](https://stackblitz.com/edit/react-embed?file=src%2FApp.tsx)** — Interactive component with location switching

## Parameters

| Parameter | Type | Required | Description | Example |
| --- | --- | --- | --- | --- |
| `lat` | Number | Yes | Latitude coordinate (`-90` to `90`) | `42.378792` |
| `lon` | Number | Yes | Longitude coordinate (`-180` to `180`) | `-83.7935` |
| `apiKey` | String | Yes | Your Precip API key | `YOUR_API_KEY` |
| `units` | String | No | Unit system (`metric` or `imperial`, default `metric`) | `imperial` |
| `widgets` | String | No | Comma-separated widget keys to show (`current`, `event`, `calendar`, `cumulative`, `total`, `precip`, `table`, `wind`, `temp`, `soiltemp`, `soilmoisture`, `snow`). Omit to show all. | `current,temp,precip` |

## Simple HTML Embed

```html
<iframe
  src="https://api.precip.ai/embed/location?lat=42.378792&lon=-83.7935&units=imperial&apiKey=YOUR_API_KEY"
  style="width: 100%; height: 400px; border: 1px solid #ddd; border-radius: 8px;"
  frameborder="0"
  allowtransparency="true"
>
</iframe>
```

## React Component Example

```jsx
import React, { useState } from "react";

const WeatherWidget = ({
  apiKey,
  initialLat = 42.378792,
  initialLon = -83.7935,
  initialUnits = "imperial",
}) => {
  const [lat, setLat] = useState(initialLat);
  const [lon, setLon] = useState(initialLon);
  const [units, setUnits] = useState(initialUnits);

  const embedUrl = `https://api.precip.ai/embed/location?lat=${lat}&lon=${lon}&units=${units}&apiKey=${encodeURIComponent(apiKey)}`;

  const locations = [
    { name: "Detroit", lat: 42.378792, lon: -83.7935 },
    { name: "Miami", lat: 25.7617, lon: -80.1918 },
    { name: "Seattle", lat: 47.6062, lon: -122.3321 },
    { name: "Denver", lat: 39.7392, lon: -104.9903 },
  ];

  return (
    <div style={{ width: "100%", maxWidth: "800px", margin: "0 auto" }}>
      <div style={{ marginBottom: "20px", textAlign: "center" }}>
        <h2>Weather Dashboard</h2>

        <div style={{ marginBottom: "10px" }}>
          {locations.map((location) => (
            <button
              key={location.name}
              onClick={() => {
                setLat(location.lat);
                setLon(location.lon);
              }}
              style={{
                margin: "0 5px",
                padding: "8px 16px",
                backgroundColor: "#007bff",
                color: "white",
                border: "none",
                borderRadius: "4px",
                cursor: "pointer",
              }}
            >
              {location.name}
            </button>
          ))}
        </div>

        <button
          onClick={() => setUnits(units === "imperial" ? "metric" : "imperial")}
          style={{
            padding: "8px 16px",
            backgroundColor: "#28a745",
            color: "white",
            border: "none",
            borderRadius: "4px",
            cursor: "pointer",
          }}
        >
          Units: {units === "imperial" ? "Imperial" : "Metric"}
        </button>
      </div>

      <iframe
        src={embedUrl}
        style={{
          width: "100%",
          height: "500px",
          border: "none",
          borderRadius: "8px",
          boxShadow: "0 4px 6px rgba(0, 0, 0, 0.1)",
        }}
        title="Weather Widget"
      />
    </div>
  );
};

export default WeatherWidget;
```

## Vue.js Component Example

```html
<template>
  <div class="weather-widget-container">
    <h2>Weather Information</h2>

    <div class="controls">
      <select v-model="selectedLocation" @change="updateLocation">
        <option
          v-for="location in locations"
          :key="location.name"
          :value="location"
        >
          {{ location.name }}
        </option>
      </select>

      <button @click="toggleUnits" class="units-toggle">
        {{ units === "imperial" ? "Switch to Metric" : "Switch to Imperial" }}
      </button>
    </div>

    <iframe
      :src="embedUrl"
      class="weather-iframe"
      frameborder="0"
      title="Weather Widget"
    />
  </div>
</template>

<script>
  export default {
    name: "WeatherWidget",
    props: {
      apiKey: { type: String, required: true },
    },
    data() {
      return {
        lat: 42.378792,
        lon: -83.7935,
        units: "imperial",
        locations: [
          { name: "Detroit, MI", lat: 42.378792, lon: -83.7935 },
          { name: "Austin, TX", lat: 30.2672, lon: -97.7431 },
          { name: "Portland, OR", lat: 45.5152, lon: -122.6784 },
          { name: "Boston, MA", lat: 42.3601, lon: -71.0589 },
        ],
        selectedLocation: null,
      };
    },
    computed: {
      embedUrl() {
        return `https://api.precip.ai/embed/location?lat=${this.lat}&lon=${this.lon}&units=${this.units}&apiKey=${encodeURIComponent(this.apiKey)}`;
      },
    },
    mounted() {
      this.selectedLocation = this.locations[0];
    },
    methods: {
      updateLocation() {
        this.lat = this.selectedLocation.lat;
        this.lon = this.selectedLocation.lon;
      },
      toggleUnits() {
        this.units = this.units === "imperial" ? "metric" : "imperial";
      },
    },
  };
</script>
```

## API Key Requirements & CORS

The embed endpoint requires a valid API key via the `apiKey` query parameter:

- **Commercial embeds (public websites or apps):** [Subscribe to a commercial plan](/pricing) and copy your key from [API Keys & Subscriptions](/subscriptions).
- **Personal / non-commercial dashboards:** Included with any [Precip app subscription](https://app.precip.ai/upgrade?upgrade_type=api_access) under **Settings → API & AI assistants**.

The endpoint allows cross-origin iframe embedding from any domain. Keep in mind that keys in client-side `iframe` URLs are visible to page visitors — contact [support@getprecip.com](mailto:support@getprecip.com) if you need domain-restricted or dedicated embed keys.
