peppermint icon indicating copy to clipboard operation
peppermint copied to clipboard

How to authenticate and use the Peppermint API externally?

Open kolevmm opened this issue 9 months ago • 1 comments

Hi Peppermint team 👋

I'm trying to integrate the Peppermint ticket system into a public website using the REST API. I have the following setup:

  • A backend user [email protected] with a valid bcrypt-hashed password inserted directly into the database.
  • I'm sending a POST request to /auth/login with the correct email and password.

However, I'm consistently getting:

{"message": "Unauthorized", "success": false}

Questions:

Is the /auth/login endpoint intended for external API authentication?

Are there any additional steps required (such as token activation, roles, etc.)?

Is there an official way to create a technical user and authenticate using a backend script?

Thanks in advance — and thanks for the amazing work on Peppermint!

kolevmm avatar Mar 30 '25 16:03 kolevmm

Its in the code, inspect the page and view the API requests on XHR/Fetch, or use something like Reqable or Postman Interceptor. It would be great to get an OpenAPI Spec however.

Here is a quick and dirty wrapper I did just clicking around and looking at XHR/Fetch.

import requests
from dotenv import load_dotenv
import os
import pprint

class PeppermintWrapper:
    
    def __init__(self, base_url: str):
        self.base_url = base_url
        self.session = requests.Session()
        self.headers = {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }

    def login(self, username: str, password: str) -> str:
        """Login to the Peppermint API and return the token."""
        url = f"{self.base_url}/auth/login"
        payload = {
            "email": username,
            "password": password
        }
        response = self.session.post(url, json=payload)
        if response.status_code == 200:
            data = response.json()
            self.session.headers.update({"Authorization": f"Bearer {data['token']}"})
            return data['token']
        else:
            raise Exception(f"Login failed: {response.status_code} - {response.text}")

    def get_self_user_data(self) -> dict:
        """Fetch user data from the Peppermint API."""
        url = f"{self.base_url}/auth/profile"
        response = self.session.get(url)
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"Failed to fetch user data: {response.status_code} - {response.text}")\
    
    def get_open_tickets(self) -> dict:
        """Fetch open tickets from the Peppermint API."""
        url = f"{self.base_url}/tickets/user/open"
        response = self.session.get(url)
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"Failed to fetch open tickets: {response.status_code} - {response.text}")




def main():
    load_dotenv()
    PEPPERMINT_UN= os.getenv("PEPPERMINT_UN")
    PEPPERMINT_PW= os.getenv("PEPPERMINT_PW")

    base_url = "http://127.0.0.1:3000/api/v1"

    peppermint = PeppermintWrapper(base_url=base_url)

    peppermint.login(PEPPERMINT_UN, PEPPERMINT_PW)
    open_tickets = peppermint.get_open_tickets()
    pprint.pprint(open_tickets,indent=4)

if __name__ == "__main__":
    main()

## example response, some payload redacted for size ##
{   'sucess': True,
    'tickets': [   {   'Number': 1,
                       'assignedTo': {   'id': '67340110-5191-470f-95e9-e51a13e36353',
                                         'name': 'admin'},
                       'client': {   'id': '60e712f8-b734-4c9c-a2e0-8b39ebc08c78',
                                     'name': 'internal',
                                     'number': '123456789'},
                       'clientId': '60e712f8-b734-4c9c-a2e0-8b39ebc08c78',
...}]}

robustgarlic avatar Apr 23 '25 13:04 robustgarlic