Creating an employee with Python

Employee management was one of the first APIs; here is how to use it with Python.

In this post we’ll go through the steps to create a new employee record attached to your Square business. If only actually hiring employees was as easy!

You’ll likely use this employee creation script as a part of some import or syncing script, but for this example we’ll make a standalone snippet that creates an employee.

# use Square's SDK to make your job easier
import squareconnect
from squareconnect.apis.v1_employees_api import V1EmployeesApi
from squareconnect.models.v1_employee import V1Employee

# setup authorization with your access token
squareconnect.configuration.access_token = 'sq0atp-XXXXXXXX'

# create an instance of the Location API class
api_instance = V1EmployeesApi()
employee = V1Employee()
employee.first_name = 'Sam'
employee.last_name = 'Smiths'
employee.email = '[email protected]'

# call the API to create the employee and print the results
api_response = api_instance.create_employee(employee)
print (api_response)

Let’s take a look at each part of the process:

import squareconnect
from squareconnect.apis.v1_employees_api import V1EmployeesApi
from squareconnect.models.v1_employee import V1Employee

First we import the squareconnect package; if you don’t have it installed already, you can use pip install squareconnect or install it directly from github pip install git+https://github.com/square/connect-python-sdk.git This package gives you access to some helper methods and models that we import in the next couple lines that make calling the APIs (and the associated HTTP requests) a little easier.

squareconnect.configuration.access_token = 'sq0atp-XXX'

Next up, you need to specify the access token that you are using to access Square’s APIs. This not only tells Square which account to do the operations on, but also makes sure that you have the appropriate permissions. You can get this token from the Square Developer Portal, and it should always be kept secret.

api_instance = V1EmployeesApi()
employee = V1Employee()
employee.first_name = 'Sam'
employee.last_name = 'Smiths'
employee.email = '[[email protected]](mailto:[email protected])'

Now that our setup is done we can create an instance of our API and a new instance of our employee model. Then you can just assign values to the different properties to fill in the information about the employee. Here I’ve added a first name, last name, and an email to the employee.

api_response = api_instance.create_employee(employee)
print (api_response)

Finally we can call the API and print out the result. If everything went well, you should see something like this:

That is it! If you try this out and have any problems, or have any comments, let us know on Twitter or just by commenting on this post!

View More Articles ›