# API Connections & Dynamic Databases

This chapter covers advanced techniques for connecting your Hub to external data sources and building dynamic, automatically-updating content.

```{tip}
This chapter covers foundational patterns. Additional connection types and examples will be added as teams adopt them.
```

## Overview

Beyond manually uploading data, your Hub can connect to:

| Source Type | Description | Use Case |
|-------------|-------------|----------|
| **ArcGIS Server services** | Enterprise GIS layers | Organizational data infrastructure |
| **External APIs** | REST endpoints | Third-party data feeds |
| **Cloud databases** | PostgreSQL, SQL Server | Operational databases |
| **Automated pipelines** | Scheduled data refresh | Keep content current |

## Referencing External Services

### Add a Service URL

1. **Sign in** to `https://geowb.maps.arcgis.com`
2. **Go to Content** > **My Content**
3. **Click New Item** > **URL**
4. **Enter the service URL**
5. **Select the item type** (Map Service, Feature Service, etc.)
6. **Complete metadata** as with any item
7. **Share to your Catalog Group**

```{important}
Referenced services display data from the source — changes at the source appear immediately on your Hub. Ensure the source is reliable and maintained.
```

### Supported Service Types

| Type | URL Pattern | Notes |
|------|-------------|-------|
| **ArcGIS Feature Service** | `https://server/arcgis/rest/services/.../FeatureServer` | Editable, queryable |
| **ArcGIS Map Service** | `https://server/arcgis/rest/services/.../MapServer` | Read-only tiles |
| **OGC WFS** | `https://server/wfs?service=WFS...` | Open standard |
| **OGC WMS** | `https://server/wms?service=WMS...` | Open standard |
| **GeoJSON** | `https://server/data.geojson` | Static or dynamic |

## Automated Data Refresh

### Option 1: Overwrite Hosted Feature Layer

For data that updates on a schedule:

1. **Create a Python script** to fetch and process data
2. **Use ArcGIS API for Python** to overwrite the layer
3. **Schedule the script** (Windows Task Scheduler, cron, or cloud function)

```python
# Example: Overwrite a hosted feature layer
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection

gis = GIS("https://geowb.maps.arcgis.com", "username", "password")
item = gis.content.get("item_id")
flc = FeatureLayerCollection.fromitem(item)
flc.manager.overwrite("path/to/updated_data.geojson")
```

```{warning}
Store credentials securely. Never commit passwords to version control. Use environment variables or secure credential storage.
```

### Option 2: Living Atlas / External Tiles

Some data sources provide continuously updated services:

| Source | Content | How to Add |
|--------|---------|------------|
| **Esri Living Atlas** | Global basemaps, imagery | Search in AGOL, add to map |
| **OpenStreetMap** | Roads, buildings, POIs | Reference OSM tile services |
| **Humanitarian Data** | Crisis data | HDX API connections |

## Database Connections

### ArcGIS Enterprise Geodatabase

For organizations with ArcGIS Enterprise:

1. **Register the database** with ArcGIS Server
2. **Publish as feature service**
3. **Reference in AGOL** via service URL

### Direct Database Queries

For advanced use cases (dashboards, custom apps):

| Approach | Complexity | Use Case |
|----------|------------|----------|
| **ArcGIS Insights** | Medium | Ad-hoc analysis |
| **Experience Builder** | Medium | Custom data widgets |
| **Custom web app** | High | Full control |

## Best Practices

### Data Reliability

| Consideration | Recommendation |
|---------------|----------------|
| **Source uptime** | Only reference reliable, maintained services |
| **Update frequency** | Document expected refresh rate |
| **Fallback plan** | Have static backup for critical data |

### Performance

| Issue | Solution |
|-------|----------|
| **Slow loading** | Use cached tile services for basemaps |
| **Large datasets** | Implement pagination or extent filtering |
| **Many concurrent users** | Consider dedicated hosting |

### Security

| Requirement | Approach |
|-------------|----------|
| **Authentication** | Use token-based auth for secure services |
| **Sensitive data** | Never expose internal APIs publicly |
| **Credential management** | Use secure vaults, not plaintext |

## Troubleshooting

| Issue | Cause | Solution |
|-------|-------|----------|
| **Service not displaying** | CORS restrictions | Check server configuration |
| **Data not updating** | Cache issues | Clear browser/AGOL cache |
| **Authentication errors** | Expired tokens | Refresh credentials |
| **Slow performance** | Large unoptimized data | Add indexes, simplify geometry |

## Related Resources

- {doc}`add-content` — Standard content publishing
- [ArcGIS API for Python](https://developers.arcgis.com/python/)
- [ArcGIS REST API](https://developers.arcgis.com/rest/)
