### Initialize MailerLite Client and Get Groups/Fields Source: https://developers-classic.mailerlite.com/docs/introduction Use this snippet to initialize the MailerLite client and fetch all available groups and fields. Ensure you have the SDK installed via Composer and replace 'your-api-key' with your actual MailerLite API key. ```php groups(); $groups = $groupsApi->get(); // returns array of groups $fieldsApi = $mailerliteClient->fields(); $fields = $fieldsApi->get(); // returns array of fields ``` -------------------------------- ### Error Response Example Source: https://developers-classic.mailerlite.com/docs/response This is an example of an error response from the API. It includes an error code and a human-readable message. ```json { "error": { "code": 123, "message": "Group not found" } } ``` -------------------------------- ### Authentication errors Source: https://developers-classic.mailerlite.com/docs/authentication These are examples of HTTP responses you might receive when authentication fails due to an invalid or missing API key. ```json { "error": { "code": 1, "message": "Unauthorized" } } ``` ```json { "error": { "code": 302, "message": "API-Key Unauthorized" } } ``` -------------------------------- ### Authenticated request example Source: https://developers-classic.mailerlite.com/docs/authentication This example demonstrates how to make a cURL request to the MailerLite API with the necessary authentication header. ```curl curl -v https://api.mailerlite.com/api/v2 \ -H "X-MailerLite-ApiKey: {replace-it-with-your-api-key}" \ -H "Content-Type: application/json" ``` -------------------------------- ### Example Webhook Payload Structure Source: https://developers-classic.mailerlite.com/docs/webhooks This JSON structure represents a typical payload received for a 'subscriber.create' event. It includes account details, event data, timestamp, event type, and webhook ID. ```json { "events": [ { "account_id": 334443, "data": { "subscriber": { "clicked": 0, "date_created": "2017-05-23 14:50:03", "date_subscribe": null, "date_unsubscribe": null, "date_updated": null, "email": "randomguy+wh1@mailerlite.com", "fields": [ { "key":"email", "value":"randomguy+wh1@mailerlite.com", "type":"TEXT" }, { "key":"name", "value":"Guy", "type":"TEXT" }, { "key":"last_name", "value":"Random", "type":"TEXT" } ], "id": 2300951083, "name": "Guy", "opened": 0, "sent": 0, "type": "active" } }, "timestamp": 1495551003, "type": "subscriber.create", "webhook_id": 2 } ] } ``` -------------------------------- ### Authenticated Request Example Source: https://developers-classic.mailerlite.com/docs/authentication Use this cURL command to make an authenticated request to the MailerLite API. Replace '{replace-it-with-your-api-key}' with your actual API key. ```curl curl -v https://api.mailerlite.com/api/v2 \ -H "X-MailerLite-ApiKey: {replace-it-with-your-api-key}" -H "Content-Type: application/json" ``` -------------------------------- ### Access Single MailerLite Group API Source: https://developers-classic.mailerlite.com/docs/introduction This example shows how to access a specific API endpoint, like the groups API, to retrieve all groups or a single group by its ID. Replace 'your-api-key' with your actual API key and '123' with the desired group ID. ```php groups(); $allGroups = $groupsApi->get(); // returns array of groups $groupId = 123; $singleGroup = $groupsApi->find($groupId); // returns single item object ``` -------------------------------- ### count() Source: https://developers-classic.mailerlite.com/docs/getting-count-of-items Get a count of items. ```APIDOC ## count() ### Description Get a count of items. ### Method GET ### Endpoint /items/count ### Response #### Success Response (200) - **count** (int) - The total number of items. ``` -------------------------------- ### HTTP Methods Source: https://developers-classic.mailerlite.com/docs/request Overview of the supported HTTP methods and their general purpose. ```APIDOC ## HTTP Methods | Verb | Description | |--------|-------------------------------------------------| | GET | Obtain information. Query path parameters are allowed. | | POST | Add new information. Body is allowed. | | PUT | Modify existing information. Body is allowed. | | DELETE | Remove information. | ``` -------------------------------- ### Limit and Offset for Pagination Source: https://developers-classic.mailerlite.com/docs/parameters Use limit and offset methods to implement pagination for API requests. Chain these methods to fetch data in chunks. ```php groups(); $items = $groupsApi->limit(10)->get(); // the first ten items (page one) $items = $groupsApi->limit(10)->offset(10)->get(); // page two $items = $groupsApi->limit(10)->offset(20)->get(); // page three ``` -------------------------------- ### Order By Source: https://developers-classic.mailerlite.com/docs/parameters Sort the returned data by a specified column in either ascending or descending order. ```APIDOC ## Order By ### Description Sorts the results based on a specified column and order (ascending or descending). ### Method - `orderBy(string $column, string $order)`: Specifies the column to sort by and the order. - `$column`: The name of the column to sort. - `$order`: The sort order, either 'ASC' (ascending) or 'DESC' (descending). ### Example ```php groups(); // Order by name in descending order $items = $groupsApi->orderBy('name', 'DESC')->get(); // Order by name in ascending order $items = $groupsApi->orderBy('name', 'ASC')->get(); ``` ``` -------------------------------- ### Acceptable Content Types Source: https://developers-classic.mailerlite.com/docs/request Specifies the content types accepted for POST and PUT requests. JSON is the default and recommended. ```APIDOC ## Acceptable Content Types We accept the following `Content-Type` header values when using `POST` or `PUT` methods: * `application/json` (this is the default and is recommended) * `multipart/form-data` * `application/x-www-form-urlencoded` ``` -------------------------------- ### Limit & Offset Pagination Source: https://developers-classic.mailerlite.com/docs/parameters Control the number of items returned and skip a certain number of items for pagination. Useful for retrieving data in chunks. ```APIDOC ## Limit & Offset ### Description Allows for pagination of results by specifying the number of items to return (`limit`) and the number of items to skip (`offset`). ### Methods - `limit(int $limit)`: Sets the maximum number of items to return. - `offset(int $offset)`: Sets the number of items to skip from the beginning of the result set. ### Example ```php groups(); // Fetch the first ten items (page one) $items = $groupsApi->limit(10)->get(); // Fetch the next ten items (page two) $items = $groupsApi->limit(10)->offset(10)->get(); // Fetch the next ten items (page three) $items = $groupsApi->limit(10)->offset(20)->get(); ``` ``` -------------------------------- ### How to authenticate Source: https://developers-classic.mailerlite.com/docs/authentication All requests to the MailerLite API must include an API key in the HTTP header for authentication. ```APIDOC ## Authentication Header Every request to MailerLite API should have an HTTP header containing a valid API key that we use to authenticate the account: `X-MailerLite-ApiKey` - your account's API key. API key can be obtained from the **Integrations** page when you are logged into the MailerLite application or by visiting [this link](https://app.mailerlite.com/integrations/api/). ``` -------------------------------- ### HTTP Response for Unauthorized (Missing API Key) Source: https://developers-classic.mailerlite.com/docs/authentication This JSON response indicates that the API key was not provided in the request header. ```json { "error": { "code": 1, "message": "Unauthorized" } } ``` -------------------------------- ### Order Data by Column Source: https://developers-classic.mailerlite.com/docs/parameters Sort your API results by a specific column in ascending or descending order. Use the orderBy method with the column name and desired order. ```php groups(); $items = $groupsApi->orderBy('name', 'DESC')->get(); // order by name descending $items = $groupsApi->orderBy('name', 'ASC')->get(); // order by name ascending ``` -------------------------------- ### HTTP Response for Unauthorized (Invalid API Key) Source: https://developers-classic.mailerlite.com/docs/authentication This JSON response is returned when the provided API key is not valid or does not have the necessary permissions. ```json { "error": { "code": 302, "message": "API-Key Unauthorized" } } ``` -------------------------------- ### Filter with Operators Source: https://developers-classic.mailerlite.com/docs/parameters Apply various comparison operators like greater than, less than, or like when filtering data. Use nested arrays in the where method to specify the operator and value. ```php groups(); $items = $groupsApi->where([ 'active' => [ '$gte' => 4 ] ])->get(); // get groups which have 4 or more active subscribers ``` -------------------------------- ### Filter by Exact Field Value Source: https://developers-classic.mailerlite.com/docs/parameters Retrieve items that match an exact value for a specific field. Pass an associative array to the where method where keys are column titles and values are the desired data. ```text groups(); $items = $groupsApi->where(['date_created' => '2016-01-01'])->get(); // get groups which have 4 active subscribers ``` -------------------------------- ### Base URL Source: https://developers-classic.mailerlite.com/docs/request The base URL for all MailerLite API v2 requests. Always use HTTPS. ```APIDOC ## Base URL https://api.mailerlite.com/api/v2/ ``` -------------------------------- ### Where Clause Filtering Source: https://developers-classic.mailerlite.com/docs/parameters Filter results based on exact field values or using comparison operators for more complex queries. ```APIDOC ## Where Clause Filtering ### Description Filters the results based on specified criteria. You can filter by exact field values or use operators for greater flexibility. ### Method - `where(array $filters)`: Applies filters to the query. ### Parameters #### Filters (Exact Match) - `['column_title' => $value]`: Filters for items where `column_title` exactly matches `$value`. ### Example (Exact Match) ```php groups(); // Get groups created on a specific date $items = $groupsApi->where(['date_created' => '2016-01-01'])->get(); ``` ### Operators Supports the following operators for filtering: - `$gt` (greater than) - `$gte` (greater than or equal) - `$lt` (less than) - `$lte` (less than or equal) - `$ne` (not equal) - `$like` (like) #### Filters (With Operators) - `['column_title' => ['operator' => $value]]`: Filters using a specific operator. ### Example (With Operators) ```php groups(); // Get groups with 4 or more active subscribers $items = $groupsApi->where([ 'active' => [ '$gte' => 4 ] ])->get(); ``` ``` -------------------------------- ### Error Response Structure Source: https://developers-classic.mailerlite.com/docs/response All error responses from the MailerLite API follow a consistent JSON structure. ```APIDOC ## Response with error Every error has the structure described below. ### Error Object | Parameter | Type | Description | |---------------|-----------|----------------------------------------------| | error | `Object` | Object that contains data about error | | error.code | `Integer` | *Optional* Code of error | | error.message | `String` | Readable message about the error occurred | ### Error Response Example ```json { "error": { "code": 123, "message": "Group not found" } } ``` ``` -------------------------------- ### API Error Codes Source: https://developers-classic.mailerlite.com/docs/response A list of specific error codes returned by the MailerLite API for various issues. ```APIDOC ## API error codes | Error code | Message | Description | |------------|----------------------|------------------------------------------------------| | 1 | Unauthorized | API key is required and is not provided | | 2 | Endpoint not found | The endpoint you are trying to use is non-existing | | 302 | API-Key Unauthorized | You are trying to use an invalid API key to authorize | | 429 | Too many requests | You reached the API rate limit | ``` -------------------------------- ### HTTP Status Codes Source: https://developers-classic.mailerlite.com/docs/response MailerLite uses standard HTTP response codes to indicate the success or failure of a request. ```APIDOC ## HTTP Status Codes MailerLite uses standard HTTP response codes. ### Status Codes | HTTP code | Description | Description | |-----------|------------------|------------------------------------------------------------------------------------------------------------| | 200 | OK | The request succeeded. | | 201 | Created | The request was fulfilled and resulted in a new resource being created. | | 204 | No Content | The server fulfilled the request but does not need to return an entity-body, i.e. when resource is deleted. | | 400 | Bad Request | The request could not be understood by the server due to malformed syntax. | | 401 | Unauthorized | The request requires user authentication. | | 404 | Not Found | The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. | | 500 | Internal Server Error | The server encountered an unexpected condition which prevented it from fulfilling the request. | ``` -------------------------------- ### PHP Function to Generate Webhook Signature Source: https://developers-classic.mailerlite.com/docs/webhooks This PHP function generates a base64 encoded HMAC-SHA256 signature for webhook payload verification. It uses the JSON payload and your MailerLite API key as the secret. ```php