> ## Documentation Index
> Fetch the complete documentation index at: https://docs.8space.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Lead Collection

> Store lead email addresses from the landing page

## POST /api/lead

Store lead email addresses submitted through the landing page. This endpoint is triggered by the `ButtonLead` component.

### Request

<ParamField body="email" type="string" required>
  Email address of the lead. Must be a valid email format.
</ParamField>

### Response

<ResponseField name="success" type="object">
  Empty object returned on successful lead storage.
</ResponseField>

<ResponseField name="error" type="string">
  Error message returned when request fails.
</ResponseField>

### Code Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://8space.app/api/lead \
    -H "Content-Type: application/json" \
    -d '{
      "email": "user@example.com"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://8space.app/api/lead', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      email: 'user@example.com'
    })
  });

  const data = await response.json();
  ```

  ```typescript TypeScript theme={null}
  interface LeadRequest {
    email: string;
  }

  const response = await fetch('https://8space.app/api/lead', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      email: 'user@example.com'
    } as LeadRequest)
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error);
  }
  ```
</CodeGroup>

### Request Validation

The endpoint validates the following:

* **Email presence**: The `email` field must be provided in the request body
* **Email format**: Must be a valid email address format

<Accordion title="Validation Code">
  ```typescript packages/landing/app/api/lead/route.ts theme={null}
  export async function POST(req: NextRequest) {
    const body = await req.json();

    if (!body.email) {
      return NextResponse.json({ error: "Email is required" }, { status: 400 });
    }

    try {
      // TODO: Add your own lead storage logic here
      // For instance, save to Supabase or send a welcome email
      return NextResponse.json({});
    } catch (e: any) {
      console.error(e);
      return NextResponse.json({ error: e.message }, { status: 500 });
    }
  }
  ```
</Accordion>

### Error Handling

<ResponseField name="400" type="error">
  **Validation Error**

  Returned when:

  * Email field is missing from request body

  ```json theme={null}
  {
    "error": "Email is required"
  }
  ```
</ResponseField>

<ResponseField name="500" type="error">
  **Internal Server Error**

  Returned when:

  * Database operation fails
  * Unexpected server error occurs

  ```json theme={null}
  {
    "error": "Error message details"
  }
  ```
</ResponseField>

### Success Response

**Status Code:** `200 OK`

```json theme={null}
{}
```

### Implementation Notes

* The current implementation includes a TODO for adding custom lead storage logic
* Consider integrating with Supabase for lead storage or sending welcome emails
* All errors are logged to the console for debugging
