# Provider API Reference

API documentation for creating and using infrastructure providers.

## Overview

Providers handle cloud-specific operations and resource provisioning. The provisioning platform supports multiple cloud providers through a unified API.

## Supported Providers

- **UpCloud** - European cloud provider
- **AWS** - Amazon Web Services
- **Local** - Local development environment

## Provider Interface

All providers must implement the following interface:

### Required Functions

```nushell
# Provider initialization
export def init [] -> record { ... }

# Server operations
export def create-servers [plan: record] -> list { ... }
export def delete-servers [ids: list] -> bool { ... }
export def list-servers [] -> table { ... }

# Resource information
export def get-server-plans [] -> table { ... }
export def get-regions [] -> list { ... }
export def get-pricing [plan: string] -> record { ... }
```plaintext

### Provider Configuration

Each provider requires configuration in Nickel format:

```nickel
# Example: UpCloud provider configuration
{
  provider = {
    name = "upcloud",
    type = "cloud",
    enabled = true,
    config = {
      username = "{{env.UPCLOUD_USERNAME}}",
      password = "{{env.UPCLOUD_PASSWORD}}",
      default_zone = "de-fra1",
    },
  }
}
```plaintext

## Creating a Custom Provider

### 1. Directory Structure

```plaintext
provisioning/extensions/providers/my-provider/
├── nulib/
│   └── my_provider.nu          # Provider implementation
├── schemas/
│   ├── main.ncl                # Nickel schema
│   └── defaults.ncl            # Default configuration
└── README.md                   # Provider documentation
```plaintext

### 2. Implementation Template

```nushell
# my_provider.nu
export def init [] {
    {
        name: "my-provider"
        type: "cloud"
        ready: true
    }
}

export def create-servers [plan: record] {
    # Implementation here
    []
}

export def list-servers [] {
    # Implementation here
    []
}

# ... other required functions
```plaintext

### 3. Nickel Schema

```nickel
# main.ncl
{
  MyProvider = {
    # My custom provider schema
    name | String = "my-provider",
    type | String | "cloud" | "local" = "cloud",
    config | MyProviderConfig,
  },

  MyProviderConfig = {
    api_key | String,
    region | String = "us-east-1",
  },
}
```plaintext

## Provider Discovery

Providers are automatically discovered from:

- `provisioning/extensions/providers/*/nu/*.nu`
- User workspace: `workspace/extensions/providers/*/nu/*.nu`

```bash
# Discover available providers
provisioning module discover providers

# Load provider
provisioning module load providers workspace my-provider
```plaintext

## Provider API Examples

### Create Servers

```nushell
use my_provider.nu *

let plan = {
    count: 3
    size: "medium"
    zone: "us-east-1"
}

create-servers $plan
```plaintext

### List Servers

```nushell
list-servers | where status == "running" | select hostname ip_address
```plaintext

### Get Pricing

```nushell
get-pricing "small" | to yaml
```plaintext

## Testing Providers

Use the test environment system to test providers:

```bash
# Test provider without real resources
provisioning test env single my-provider --check
```plaintext

## Provider Development Guide

For complete provider development guide, see:

- **[Provider Development](../development/QUICK_PROVIDER_GUIDE.md)** - Quick start guide
- **[Extension Development](../development/extensions.md)** - Complete extension guide
- **[Integration Examples](integration-examples.md)** - Example implementations

## API Stability

Provider API follows semantic versioning:

- **Major**: Breaking changes
- **Minor**: New features, backward compatible
- **Patch**: Bug fixes

Current API version: `2.0.0`

---

For more examples, see [Integration Examples](integration-examples.md).
