PHPackages                             koriym/env-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. koriym/env-json

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

koriym/env-json
===============

Type-safe, schema-driven alternative to .env files with JSON Schema validation

1.0.1(10mo ago)5189.3k—4.2%51MITPHPPHP ^8.1CI passing

Since Dec 5Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/koriym/Koriym.EnvJson)[ Packagist](https://packagist.org/packages/koriym/env-json)[ RSS](/packages/koriym-env-json/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (7)Used By (1)

Koriym.EnvJson
==============

[](#koriymenvjson)

[![Continuous Integration](https://github.com/koriym/Koriym.EnvJson/actions/workflows/continuous-integration.yml/badge.svg?branch=1.x)](https://github.com/koriym/Koriym.EnvJson/actions/workflows/continuous-integration.yml)[![codecov](https://camo.githubusercontent.com/1e22db0837952382078a325ad7a1381827c412beb205f0e7a46a8033665ce0fd/68747470733a2f2f636f6465636f762e696f2f67682f6b6f7269796d2f4b6f7269796d2e456e764a736f6e2f67726170682f62616467652e7376673f746f6b656e3d515451656e70696a6771)](https://codecov.io/gh/koriym/Koriym.EnvJson)[![Type Coverage](https://camo.githubusercontent.com/96abbc34a0a042ed876d5ba0b4d469865bd6b0e4f64b516dcc8a265628b29074/68747470733a2f2f73686570686572642e6465762f6769746875622f6b6f7269796d2f4b6f7269796d2e456e764a736f6e2f636f7665726167652e737667)](https://shepherd.dev/github/koriym/Koriym.EnvJson)

A modern approach to environment variables using JSON instead of `.env` files, with built-in validation via JSON Schema.

- Documentation: [English](https://koriym.github.io/Koriym.EnvJson/) | [Japanese](https://koriym.github.io/Koriym.EnvJson/README.ja)

[![env.json logo](https://camo.githubusercontent.com/42a4f9f65d6dda8089e5cac85245502bcfc0508026cea185e78f3ae400c28430/68747470733a2f2f6b6f7269796d2e6769746875622e696f2f4b6f7269796d2e456e764a736f6e2f696d616765732f73746f72792f6a61312e6a7067)](https://camo.githubusercontent.com/42a4f9f65d6dda8089e5cac85245502bcfc0508026cea185e78f3ae400c28430/68747470733a2f2f6b6f7269796d2e6769746875622e696f2f4b6f7269796d2e456e764a736f6e2f696d616765732f73746f72792f6a61312e6a7067)

Installation
------------

[](#installation)

```
composer require koriym/env-json
```

Basic Usage
-----------

[](#basic-usage)

```
// Load and validate environment variables
$env = (new EnvJson())->load(__DIR__);

// Access variables
echo $env->DATABASE_URL;
echo getenv('DATABASE_URL');
```

Configuration Files
-------------------

[](#configuration-files)

### env.schema.json

[](#envschemajson)

```
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["DATABASE_URL", "API_KEY"],
    "properties": {
        "DATABASE_URL": {
            "description": "Database connection string",
            "pattern": "^mysql://.*"
        },
        "API_KEY": {
            "description": "API authentication key",
            "minLength": 32
        },
        "DEBUG_MODE": {
            "description": "Enable debug output",
            "enum": ["true", "false"],
            "default": "false"
        }
    }
}
```

### env.json

[](#envjson)

```
{
    "$schema": "./env.schema.json",
    "DATABASE_URL": "mysql://user:pass@localhost/mydb",
    "API_KEY": "1234567890abcdef1234567890abcdef",
    "DEBUG_MODE": "true"
}
```

⚠️ Important: Environment variables are always strings
------------------------------------------------------

[](#️-important-environment-variables-are-always-strings)

Do not use `"type": "boolean"` or `"type": "integer"` in your schema. Use `"enum": ["true", "false"]` for booleans and `"pattern": "^[0-9]+$"` for numbers.

Converting from .env
--------------------

[](#converting-from-env)

```
bin/ini2json .env
```

This generates both `env.schema.json` and `env.json` files.

CLI Tool
--------

[](#cli-tool)

```
# Load variables into current shell
source
