PHP (Laravel) Setup
Upgrading to v2.0?
Please see our upgrade path documentation.
Any issues?
Integrations can be tricky! Contact support if you have any questions/issues.
Overview
If you're a developer, it takes a few small steps to send your API logs to ReadMe so your team can get deep insights into your API's usage with ReadMe Metrics. Here's an overview of how the integration works:
- You add the ReadMe middleware to your Laravel application.
- The middleware sends to ReadMe the response object that your Laravel application generates each time a user makes a request to your API. The entire response is sent, unless you blacklist or whitelist keys.
- ReadMe populates Metrics with this information, such as which endpoint is being called, response code, and error messages. It also identifies the customer who called your API, using whichever keys in the middleware you call out as containing relevant customer info.
Steps
Note these steps assume you are working in PHP and the Laravel web framework
- From the directory of your company's API codebase, run the following command in your command line to install the
readme/metrics
package from Packagist:
composer require readme/metrics
- Install the configuration file for
readme/metrics
, namedreadme.php
, into your application with the following command:
php artisan vendor:publish --provider="ReadMe\ServiceProvider"
- Locate the "kernel" file for your API routing layer, this will typically be
app/Http/Kernel.php
, and search for aapi
group under$middlewareGroups
. Install the\Readme\Middleware
Service Provider into that array. It should look something like the following:
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
// Any web middleware you may have set up.
],
'api' => [
\ReadMe\Middleware::class,
// Any other API middleware you may have set up.
],
];
- Configure the middleware in the newly created configuration file from step 2:
config/readme.php
:- Change
YOUR README API KEY
to API_KEY. If you're currently logged into these docs, you can see your ReadMe API key in the preceding sentence; otherwise, you can find it athttps://dash.readme.com/project/YOUR PROJECT/api-key
.
- Change
- Modify the
constructGroup
function in the newly createdapp/Handler/ReadMe.php
handler to return data specific to your API. See Identfying the API Caller for detailed instructions.
Identifying the API Caller
There are three pieces of data you can use to identify the user making the API call. (If your Laravel request object doesn't have all this information, we recommend adding it via additional middleware prior to this.) We recommend supplying all three to get the most insights about your API.
<?php
namespace App\Handler;
use Illuminate\Http\Request;
class ReadMe implements \ReadMe\Handler
{
public static function constructGroup(Request $request): array
{
$user = $request->user();
if (!$user) {
return [
'api_key' => session()->getId()
];
}
return [
'api_key' => $user->id,
'label' => $user->name,
'email' => $user->email
];
}
}
Field | Usage |
---|---|
api_key | Required API Key used to make the request. |
label | This will be used to identify the user on ReadMe, since it's much easier to remember a name than a unique identifier. |
email | Email of the person that is making the call. |
Configuration Options
There are a few configurable options in config/readme.php
that let you change how logs are sent to ReadMe.
Option | Type | Description |
---|---|---|
development_mode | bool | When disabled, development_mode will make all API requests asynchronously as well as silencing all possible errors in transit. This defaults to true , and you should change it to false before deploying your integration to production. |
blacklist | Array of strings | An array of keys from your API requests and responses that you wish to blacklist from sending to ReadMe. If this option is present, it will be favored over the whitelist option below. |
whitelist | Array of strings | An array of keys from your API requests and responses that you only wish to send to ReadMe. |
base_log_url | string | This is the base URL for your ReadMe project. Normally this would be https://projectName.readme.io or https://docs.yourdomain.com , however if this value is not supplied, a request to the ReadMe API will be made once a day to retrieve it. This data is cached into $COMPOSER_HOME/cache . |
blacklist
andwhitelist
do not support support dot-notation so only top-level keys can be specified.
Sample Applications
FAQ
How can I upgrade to v2.0?
- Copy
config/readme.php
toconfig\readme.php.backup
. - Update the SDK:
composer update readme/metrics
- Re-run the config publishing step from our installation instructions:
php artisan vendor:publish --provider="ReadMe\ServiceProvider"
This will install the new config and the new handler class.
- Open up
config\readme.php.backup
and copy yourapi_key
,development_mode
,blacklist
, andwhitelist
configs over toconfig\readme.php
. - Open up
app\Handler\Readme.php
and copy the contents of the old group function fromconfig\readme.php.backup
into theconstructGroup
function.
Updated 5 months ago