PHPackages                             ocolin/easy-swagger - 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. [API Development](/categories/api)
4. /
5. ocolin/easy-swagger

ActiveLibrary[API Development](/categories/api)

ocolin/easy-swagger
===================

Basic REST client that reads Swagger file

2.10(6mo ago)05911MITPHPPHP &gt;=8.3

Since Aug 23Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/ocolin/EasySwagger)[ Packagist](https://packagist.org/packages/ocolin/easy-swagger)[ RSS](/packages/ocolin-easy-swagger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (23)Used By (1)

EasySwagger
===========

[](#easyswagger)

This is a library for creating quick REST clients for APIs using Swagger.

How it works
============

[](#how-it-works)

You will need to point to a Swagger JSON file containing the Swagger rules, properties, etc. You also need to provide the URI information as well as authentication credentials.

Once you have these, you can past the end point URI into the path, as well as any parameters needed to make your request. Any URL query parameters will be replaced with properties in your data object or array.

Constructor properties
----------------------

[](#constructor-properties)

- $host - The API server name. Example:
- $api\_file - Path to your Swagger JSON config file. Example: \_\_DIR \_\_ . '/api.v1.json'
- $base\_uri - Directory path to the REST API root on server. Example: /api/v1/.
- $env\_file - File path to your environment file. This is if you want to use environment variables instead of constructor arguments.
- $token - Your authentication token if your API uses a token in the HTTP header.
- $token\_name - The name of the HTTP header to use for your auth token if your API uses them.
- $auth\_method - Method of authentication. 'token' for HTTP header tokens, or 'basicauth' for using HTTP Basic Auth.
- $username - Username for HTTP Basic Auth
- $password - Password for HTTP Basic Auth
- $env\_prefix - Because a program may use this library for multiple API servers at once, the different servers can be specified with different environment variable prefixes. See the section on Environment variables.
- $standalone - If you are using this library as a stand along program, this will check for a local .env file to load variables from.

Environment variables
---------------------

[](#environment-variables)

Instead of passing constructor arguments to this library, you can instead load them as environment variables. To handle working with multiple REST APIs, the variable names require a prefix to seperate them from each other. So for example you can have multiple host names in the same environment using different prefixes. In the examples we will use the prefix 'SWAGGER', but you can specify the prefix using the argument $env\_prefix. 'SWAGGER' is then default prefix if you do not specify one. So if you only are using one API and don't need a prefix, you can name them similar to this example:

- SWAGGER\_FILE - File path to your Swagger JSON config file. Example "/dir/api.v2.1.json".
- SWAGGER\_HOST - The hostname of the REST server. Example: .
- SWAGGER\_BASE\_URI - The Directory of the URI to the API root. Example: /api/v1/.
- SWAGGER\_TOKEN - The authentication token for the server. This is used for token based authentication. It is not used with Basic Auth.
- SWAGGER\_TOKEN\_NAME - The HTTP Header name to use for your token (or key). This is only used for token based auth.
- SWAGGER\_AUTH\_METHOD - Specify if you are using 'token' based authentication, or 'basicauth' for HTTP Basic Auth style authentication.
- SWAGGER\_USERNAME - The username for HTTP Basic Auth authentication.
- SWAGGER\_PASSWORD - The password for HTTP Basic Auth authentication.

### Example of envrionment variables with a prefix and their defaults:

[](#example-of-envrionment-variables-with-a-prefix-and-their-defaults)

- PREFIX\_FILE - Required via env or constructor
- PREFIX\_HOST - Required via env or constructor
- PREFIX\_BASE\_URI - Not needed for every API
- PREFIX\_AUTH\_METHOD - Defaults to 'token' mode
- PREFIX\_TOKEN\_NAME - Defaults to 'x-auth-token'

Examples
--------

[](#examples)

### Usage Example using constructor arguments

[](#usage-example-using-constructor-arguments)

```
$client = new Swagger(
    host: 'https://api.server.com',
    base_uri: '/api/v1/',
    api_file: __DIR__ . '/mySwagger.api.json',
    token: 'KJHsdhjkjh987sjskjh',
    token_name: 'X-Auth-Key',
);

```

### Usage example using environment for construction

[](#usage-example-using-environment-for-construction)

```
// SET UP ENVIRONMENT VARIABLES
$_ENV['MYAPI_HOST'] = 'https://api.server.com';
$_ENV['MYAPI_BASE_URI'] = '/dir/to/api/';
$_ENV['MYAPI_FILE'] = /dir/nameOfSwaggerFile.jon';
$_ENV['MYAPI_TOKEN'] = 'LKSJdkjskjd898hJHdkjjfh87';
$_ENV['MYAPI_TOKEN_NAME'] = 'X-Auth-Key';

$client = new Swagger( env_prefix: 'MYAPI' );

```

### Usage example of loading environment from calling app.

[](#usage-example-of-loading-environment-from-calling-app)

```
$client = new Swagger( env_prefix: 'MYAPI' );

```

### Usage example of loading specified environment file.

[](#usage-example-of-loading-specified-environment-file)

```
$client = new Swagger(
    env_file: '/dir/to/my/.env'
    env_prefix: 'MYAPI',
);

```

### Usage example using Basic Auth via constructor arguments

[](#usage-example-using-basic-auth-via-constructor-arguments)

```
$client = new Swagger(
    host: 'https://api.server.com',
    base_uri: '/api/v1/',
    api_file: __DIR__ . '/mySwagger.api.json',
    auth_method: 'basicauth',
    username: 'bob@bob.com',
    password: 'kjhsdfs7sbjh3JKHG1'
);

```

### Usage 1 example of calling an endpoint

[](#usage-1-example-of-calling-an-endpoint)

```
$output = $client->path(
      path: '/devices',
    method: 'GET',
      data: [ 'type' => 'olt' ]
);

```

### Usage 2 example of calling an endpoint

[](#usage-2-example-of-calling-an-endpoint)

```
$output = $client->path(
      path: '/devices/{id}',
    method: 'GET',
        data: [
            'type' => 'olt',
            'id' => 'KJHjsk87JJH821jJqkslgh-98asjhCB'
        ]
);

```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance68

Regular maintenance activity

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Recently: every ~34 days

Total

22

Last Release

188d ago

Major Versions

1.5 → 2.02025-05-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/e97ac0aa452b872ddc3f7f4c56c83852574a27bb74622f8c054d11ca20008fc9?d=identicon)[Ocolin](/maintainers/Ocolin)

---

Top Contributors

[![ocolin](https://avatars.githubusercontent.com/u/8870196?v=4)](https://github.com/ocolin "ocolin (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ocolin-easy-swagger/health.svg)

```
[![Health](https://phpackages.com/badges/ocolin-easy-swagger/health.svg)](https://phpackages.com/packages/ocolin-easy-swagger)
```

###  Alternatives

[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3731.2M42](/packages/tencentcloud-tencentcloud-sdk-php)[convertkit/convertkitapi

Kit PHP SDK for the Kit API

2167.1k1](/packages/convertkit-convertkitapi)[mapado/rest-client-sdk

Rest Client SDK for hydra API

1125.9k2](/packages/mapado-rest-client-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
