Logs are very helpful for troubleshooting and debugging issues on your WordPress sites. At Kinsta, you can access three types of logs: error logs, kinsta-cache-perf (cache performance) logs, and access logs.

Accessing logs through the MyKinsta dashboard is straightforward: navigate to WordPress Sites, select the desired site, and click the Logs tab to open the Log viewer page.

Now, with the introduction of the Kinsta API, you can programmatically access these logs. As an agency, you can create custom interfaces for accessing logs, while larger teams can leverage tools like Slack to create a custom Slackbot. This bot can interact with the API through Slash commands, streamlining log retrieval and management.

This guide delves into the logs endpoint available through the API, its potential uses, and how to seamlessly access these logs within a Slack environment.

Understanding the Kinsta API

The Kinsta API is a powerful tool that allows you to interact with Kinsta services like hosted WordPress sites programmatically. It can help automate various tasks related to WordPress management, including site creation, retrieving site information, getting the status of a site, browsing and restoring backups, fetching site logs, and more.

To use Kinsta’s API, you must have an account with at least one WordPress site, application, or database in MyKinsta. You also need to generate an API key to authenticate and access your account.

To generate an API key:

  1. Go to your MyKinsta dashboard.
  2. Navigate to the API Keys page (Your name > Company settings > API Keys).
  3. Click Create API Key.
  4. Choose an expiration or set a custom start date and number of hours for the key to expire.
  5. Give the key a unique name.
  6. Click Generate.

After creating an API key, copy it and store it somewhere safe (using a password manager is recommended), as this is the only time it is revealed within MyKinsta. You can generate multiple API keys, which will be listed on the API Keys page. If you need to revoke an API key, click the Revoke button next to its name and expiry date.

Accessing Server Logs With Kinsta API

To access logs with the Kinsta API, you need to specify the site environment ID, the type of log you wish to fetch (e.g. error, access, or kinsta-cache-perf), and the number of log lines to retrieve.

Let’s explore this endpoint and later integrate it into a Slackbot so you can use Slack’s Slash commands to interact with the Kinsta API.

You can obtain your site’s environment ID programmatically through the get site environment endpoint, which returns details about your site’s environment, including its ID:


    {
      "site": {
        "environments": [
          {
            "id": "54fb80af-576c-4fdc-ba4f-b596c83f15a1",
            ...
          }
        ]
      }
    }
  

Once you have your site’s environment ID, you can then send a GET request to /sites/environments/{env_id}/logs?file_name=error&lines=100:


    curl -i -X GET 
      ' 
      -H 'Authorization: Bearer '
  

This will return a String with the specified number of log lines:


    {
        "environment": {
            "container_info": {
                "logs": "...
            }
        }
    }
  


(Remaining content is truncated)