A Python SDK for interacting with the GoHighLevel API version 2.
To authenticate with the GoHighLevel API, you can follow the OAuth flow below:
-
Install the required packages using pip:
pip install -r requirements.txt
-
You will need to create a HighLevel app and generate some IDs you will need for later. Here are the steps:
-
Navigate to https://marketplace.gohighlevel.com/ and sign up for a developer account.
-
Once you have created a developer account, go to "My Apps" and click on "Create App".
-
Give it a name, make it public or private, and choose whether you need the "Agency" or "Sub-Account" distribution type. Pick both. You won't regret it.
-
Click on the app you have just created and you will be directed to the "Settings" page and should see sections for "Scopes", "Redirect URLs", and "Client Keys".
-
First you will need to select which scopes your app will need. For example, if you want to use the "Calendar" endpoint to pull information about calendars, then you will need to choose "calendars.readonly". If you need to make updates to calendars, then choose "calendars.readonly". You can find more information about scopes here: https://highlevel.stoplight.io/docs/integrations/lgbsau1maxulb-scopes
-
After choosing the scope needed, you will need to add the scope to the config file. Navigate to
config.py
and replaceSCOPE
with the ones you have chosen:# config.py class HighLevelConfig: SCOPE = ['scopes_here']
-
Next you will need to add the following redirect URL to your app: "http://localhost:3000/oauth/callback". This is will allow you to retrieve the access token later on.
-
Navigate down to "Client Keys" and click "Add". Input a name and copy both the "Client ID" and "Client Secret". It is important that you keep these somewhere safe.
-
Navigate to
config.py
and replaceCLIENT_ID
andCLIENT_SECRET
with the one generated when creating your HighLevel app:# config.py class HighLevelConfig: CLIENT_ID = 'your_client_id' CLIENT_SECRET = 'your_client_secret'
-
Navigate into your terminal and run the following code.
python highlevel_sdk/auth.py
-
Open a web browser and navigate to
http://localhost:3000/initiate
. This will initiate the OAuth flow and redirect you to the GoHighLevel authorization page. -
After granting permission by selecting the sub-account, you will be redirected back to a new page with the following text
{
"access_token": "access_token_here",
"token_type": "Bearer",
"expires_in": 86399,
"refresh_token": "refresh_token_here",
"scope": "conversations/message.readonly conversations/message.write",
"userType": "Location",
"locationId": "location_id_here",
"companyId": "location_id_here",
"hashedCompanyId": "hashed_company_id_here",
}
You can now use this access token to authenticate your requests to the GoHighLevel API. You will want to edit the auth.py routes to save the access token to a database or file for later use. Additionally, your access token will be short lived, but can be refreshed using the refresh token.
To refresh your access token, you can use the refresh_token function in the auth.py file to get a new access token.
from auth import refresh_token
new_token = refresh_token(refresh_token)
You can also refresh the token by using the refresh_token method on any of the GoHighLevel objects in the models.py file.
from models.models import Location
token_data = {
"access_token": "initial_access_token",
"refresh_token": "initial_refresh_token",
"expires_in": 3600
}
location = Location(id="location_id_here")
location.set_token_data(token_data)
try:
location.refresh_token()
except ValueError as e:
print(e)
updated_token_data = location.get_token_data()
print(updated_token_data)
For more information about the GoHighLevel API and available endpoints, refer to the official documentation at GoHighLevel API Documentation.