> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/pocketbase/pocketbase/llms.txt
> Use this file to discover all available pages before exploring further.

# URL field

> Store and validate URLs with optional domain restrictions

The url field stores a single URL string and validates it against the standard URL format. You can optionally restrict which domains are allowed or blocked.

## Configuration options

<ParamField path="exceptDomains" type="[]string">
  List of domains (hosts) that are NOT allowed. URLs with these domains will fail validation. Cannot be set if `onlyDomains` is specified.
</ParamField>

<ParamField path="onlyDomains" type="[]string">
  List of domains (hosts) that are exclusively allowed. URLs must use one of these domains. Cannot be set if `exceptDomains` is specified.
</ParamField>

<ParamField path="required" type="bool" default="false">
  When true, requires the field value to be a non-empty, valid URL.
</ParamField>

## Validation rules

The url field validates:

* **Format**: Value must be a valid URL format (including scheme)
* **Domain allowlist**: If `onlyDomains` is set, URL host must be in the list
* **Domain blocklist**: If `exceptDomains` is set, URL host must NOT be in the list
* **Required**: If enabled, value cannot be empty

<Warning>
  You cannot set both `exceptDomains` and `onlyDomains` at the same time. Choose one approach based on your use case.
</Warning>

## Go examples

<Tabs>
  <Tab title="Basic usage">
    ```go theme={null}
    import "github.com/pocketbase/pocketbase/core"

    field := &core.URLField{
        Name:     "website",
        Required: false,
    }

    collection.Fields.Add(field)

    // Set field value
    record.Set("website", "https://example.com")
    ```
  </Tab>

  <Tab title="Restrict to specific domains">
    ```go theme={null}
    field := &core.URLField{
        Name:        "socialProfile",
        OnlyDomains: []string{"twitter.com", "linkedin.com", "github.com"},
    }

    collection.Fields.Add(field)

    // This will validate successfully
    record.Set("socialProfile", "https://github.com/username")

    // This will fail validation
    record.Set("socialProfile", "https://facebook.com/username")
    ```
  </Tab>

  <Tab title="Block specific domains">
    ```go theme={null}
    field := &core.URLField{
        Name:          "externalLink",
        ExceptDomains: []string{"malicious.com", "spam-site.net"},
    }

    collection.Fields.Add(field)

    // This will validate successfully
    record.Set("externalLink", "https://trusted-site.com")

    // This will fail validation
    record.Set("externalLink", "https://malicious.com")
    ```
  </Tab>

  <Tab title="Multiple URL fields">
    ```go theme={null}
    // Website
    collection.Fields.Add(&core.URLField{
        Name: "website",
    })

    // GitHub profile (restricted domain)
    collection.Fields.Add(&core.URLField{
        Name:        "github",
        OnlyDomains: []string{"github.com"},
    })

    record.Set("website", "https://myblog.com")
    record.Set("github", "https://github.com/myusername")
    ```
  </Tab>
</Tabs>

## Database column type

```sql theme={null}
TEXT DEFAULT '' NOT NULL
```

## Common use cases

<CodeGroup>
  ```go Social media profiles theme={null}
  field := &core.URLField{
      Name: "linkedin",
      OnlyDomains: []string{
          "linkedin.com",
          "www.linkedin.com",
      },
  }
  ```

  ```go Internal links only theme={null}
  field := &core.URLField{
      Name:        "documentLink",
      Required:    true,
      OnlyDomains: []string{"docs.company.com"},
  }
  ```

  ```go Image hosting theme={null}
  field := &core.URLField{
      Name: "avatar",
      OnlyDomains: []string{
          "imgur.com",
          "cloudinary.com",
          "cdn.myapp.com",
      },
  }
  ```
</CodeGroup>

## Domain matching

<Info>
  The domain matching is exact and includes the port if specified. For example:

  * `"example.com"` matches `https://example.com/path` but NOT `https://www.example.com/path`
  * `"example.com:8080"` matches `https://example.com:8080` but NOT `https://example.com`

  Make sure to include all variations (www, subdomains, ports) in your domain lists.
</Info>

## Best practices

<Note>
  * URLs must include the scheme (http\://, https\://, etc.)
  * Use `onlyDomains` when restricting to specific platforms or services
  * Use `exceptDomains` to block known malicious or unwanted domains
  * Domain matching includes the port number if present in the URL
  * Consider validating URL accessibility separately if needed
</Note>

## Zero value

The zero value for url fields is an empty string `""`.
