Search code examples
google-sheets-apigoogle-api-python-client

google-api-python-client best practice on creating service object


When using google-api-python-client When calling multiple methods like update, append etc. using service object created with

service = discovery.build('sheets', 'v4', credentials=credentials`)

Is it a best practice to create the service object once and reuse it throughout the program or should I create new service objects each time with discovery.build when calling an API function like service.spreadsheets().values().append,service.spreadsheets().values().get etc.

Basically my question is can I use the same service service object multiple times in the application.

What is the recommended way to do it?


Solution

  • Answer:

    As long as you are using the same API, you should use the same service object for each API method call.

    Further Information:

    The only reason you would need to make multiple service objects is if your application was using different APIs (Drive, Gmail, Sheets, etc). The service object is a build of the API interaction which can be used as many times as you like within the same application to interact with the API to which it has been built.