# spf13/viper architecture

> Architecture diagram of the GitHub repository spf13/viper, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spf13/viper
- Repository: https://github.com/spf13/viper

## Overview

Viper is a Go configuration registry: applications set or bind values, load them from multiple sources, and retrieve typed, merged configuration. The graph preserves the documented source precedence, file and buffer formats, file writing and watching, aliases, and optional remote providers. Source excerpts show the registry and codec implementations; several capability links rely on the README because the relevant runtime sections were not sampled.

Main entry points: [`viper.go`](https://github.com/spf13/viper/blob/master/viper.go) (Key lookup), [`encoding.go`](https://github.com/spf13/viper/blob/master/encoding.go) (Codec registry), [`file.go`](https://github.com/spf13/viper/blob/master/file.go) (Config file I/O), [`remote.go`](https://github.com/spf13/viper/blob/master/remote.go) (Remote source API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sources["Configuration sources"]
  node_file_io["Config file I/O<br/>[file.go]"]
  node_file_search["Config discovery<br/>[finder.go]"]
  node_environment["Environment variables"]
  node_flags["Flag bindings<br/>[flags.go]"]
end

subgraph group_registry["Configuration registry"]
  node_viper["Viper registry<br/>[viper.go]"]
  node_defaults["Defaults and overrides<br/>[viper.go]"]
  node_aliases["Key aliases<br/>[viper.go]"]
  node_key_lookup["Key lookup<br/>[viper.go]"]
  node_typed_values["Typed accessors<br/>[viper.go]"]
end

subgraph group_formats["Format codecs"]
  node_codec_registry["Codec registry<br/>[encoding.go]"]
  node_dotenv_codec["Dotenv codec<br/>[codec.go]"]
  node_json_codec["JSON codec<br/>[codec.go]"]
  node_toml_codec["TOML codec<br/>[codec.go]"]
  node_yaml_codec["YAML codec<br/>[codec.go]"]
end

subgraph group_remote["Remote configuration"]
  node_remote_api["Remote source API<br/>[remote.go]"]
  node_remote_adapter["Remote adapter<br/>[remote.go]"]
end

node_application(("Go application"))
node_remote_store[("Remote KV stores")]

node_application -->|"sets and reads"| node_viper
node_application -->|"reads and writes"| node_file_io
node_file_search -.->|"locates files"| node_file_io
node_environment -->|"provides values"| node_key_lookup
node_flags -->|"provides values"| node_key_lookup
node_defaults -->|"supplies fallback"| node_key_lookup
node_aliases -->|"resolves keys"| node_key_lookup
node_file_io -.->|"decodes and encodes"| node_codec_registry
node_codec_registry -->|"selects codec"| node_dotenv_codec
node_codec_registry -->|"selects codec"| node_json_codec
node_codec_registry -->|"selects codec"| node_toml_codec
node_codec_registry -->|"selects codec"| node_yaml_codec
node_application -->|"configures and reads"| node_remote_api
node_remote_api -->|"delegates provider calls"| node_remote_adapter
node_remote_adapter -->|"reads and watches"| node_remote_store
node_remote_api -->|"merges remote values"| node_key_lookup
node_key_lookup -->|"converts values"| node_typed_values
node_application -->|"watches changes"| node_file_io

click node_viper "https://github.com/spf13/viper/blob/master/viper.go"
click node_file_io "https://github.com/spf13/viper/blob/master/file.go"
click node_file_search "https://github.com/spf13/viper/blob/master/finder.go"
click node_flags "https://github.com/spf13/viper/blob/master/flags.go"
click node_defaults "https://github.com/spf13/viper/blob/master/viper.go"
click node_aliases "https://github.com/spf13/viper/blob/master/viper.go"
click node_key_lookup "https://github.com/spf13/viper/blob/master/viper.go"
click node_typed_values "https://github.com/spf13/viper/blob/master/viper.go"
click node_codec_registry "https://github.com/spf13/viper/blob/master/encoding.go"
click node_dotenv_codec "https://github.com/spf13/viper/blob/master/internal/encoding/dotenv/codec.go"
click node_json_codec "https://github.com/spf13/viper/blob/master/internal/encoding/json/codec.go"
click node_toml_codec "https://github.com/spf13/viper/blob/master/internal/encoding/toml/codec.go"
click node_yaml_codec "https://github.com/spf13/viper/blob/master/internal/encoding/yaml/codec.go"
click node_remote_api "https://github.com/spf13/viper/blob/master/remote.go"
click node_remote_adapter "https://github.com/spf13/viper/blob/master/remote/remote.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_file_io,node_file_search,node_environment,node_flags toneBlue
class node_viper,node_defaults,node_aliases,node_key_lookup,node_typed_values,node_remote_store toneAmber
class node_codec_registry,node_dotenv_codec,node_json_codec,node_toml_codec,node_yaml_codec toneMint
class node_remote_api,node_remote_adapter toneRose
class node_application toneIndigo
```

## Components

### Configuration sources

- [Config file I/O](https://github.com/spf13/viper/blob/master/file.go): `file.go`
- [Config discovery](https://github.com/spf13/viper/blob/master/finder.go): `finder.go`
- **Environment variables**
- [Flag bindings](https://github.com/spf13/viper/blob/master/flags.go): `flags.go`

### Configuration registry

- [Viper registry](https://github.com/spf13/viper/blob/master/viper.go): `viper.go`
- [Defaults and overrides](https://github.com/spf13/viper/blob/master/viper.go): `viper.go`
- [Key aliases](https://github.com/spf13/viper/blob/master/viper.go): `viper.go`
- [Key lookup](https://github.com/spf13/viper/blob/master/viper.go): `viper.go`
- [Typed accessors](https://github.com/spf13/viper/blob/master/viper.go): `viper.go`

### Format codecs

- [Codec registry](https://github.com/spf13/viper/blob/master/encoding.go): `encoding.go`
- [Dotenv codec](https://github.com/spf13/viper/blob/master/internal/encoding/dotenv/codec.go): `internal/encoding/dotenv/codec.go`
- [JSON codec](https://github.com/spf13/viper/blob/master/internal/encoding/json/codec.go): `internal/encoding/json/codec.go`
- [TOML codec](https://github.com/spf13/viper/blob/master/internal/encoding/toml/codec.go): `internal/encoding/toml/codec.go`
- [YAML codec](https://github.com/spf13/viper/blob/master/internal/encoding/yaml/codec.go): `internal/encoding/yaml/codec.go`

### Remote configuration

- [Remote source API](https://github.com/spf13/viper/blob/master/remote.go): `remote.go`
- [Remote adapter](https://github.com/spf13/viper/blob/master/remote/remote.go): `remote/remote.go`

### Other components

- **Go application**
- **Remote KV stores**

## Connections

- Go application → Viper registry: sets and reads (evidence: `README.md`)
- Go application → Config file I/O: reads and writes (evidence: `README.md`)
- Config discovery → Config file I/O: locates files
- Environment variables → Key lookup: provides values (evidence: `README.md`)
- Flag bindings → Key lookup: provides values (evidence: `README.md`)
- Defaults and overrides → Key lookup: supplies fallback (evidence: `README.md`)
- Key aliases → Key lookup: resolves keys (evidence: `README.md`)
- Config file I/O → Codec registry: decodes and encodes
- Codec registry → Dotenv codec: selects codec (evidence: `encoding.go`)
- Codec registry → JSON codec: selects codec (evidence: `encoding.go`)
- Codec registry → TOML codec: selects codec (evidence: `encoding.go`)
- Codec registry → YAML codec: selects codec (evidence: `encoding.go`)
- Go application → Remote source API: configures and reads (evidence: `README.md`)
- Remote source API → Remote adapter: delegates provider calls (evidence: `remote/remote.go`)
- Remote adapter → Remote KV stores: reads and watches (evidence: `remote/remote.go`)
- Remote source API → Key lookup: merges remote values (evidence: `remote.go`)
- Key lookup → Typed accessors: converts values (evidence: `viper.go`)
- Go application → Config file I/O: watches changes (evidence: `README.md`)
