Back

Step by Step Guide to Building a Looker API Integration in Python

Aug 9, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Looker API integration? You're in for a treat. Looker's API is a powerful tool that lets you programmatically access and manipulate your Looker instance. In this guide, we'll walk through building a solid integration using Python. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • A Python environment (3.6+ recommended)
  • Looker API credentials (we'll cover this in a bit)
  • requests library installed (pip install requests)

Authentication

First things first, let's get you authenticated:

  1. Log into your Looker instance
  2. Head to Admin > Users > Your User > Edit
  3. Under API3 Keys, generate a new key pair

Now you've got your client ID and secret. Keep these safe – they're your golden tickets!

Basic API Connection

Let's establish our connection:

import requests import json base_url = "https://your-instance.looker.com:19999/api/3.1" client_id = "your_client_id" client_secret = "your_client_secret" def get_auth_token(): url = f"{base_url}/login" payload = { "client_id": client_id, "client_secret": client_secret } response = requests.post(url, data=json.dumps(payload)) return response.json()["access_token"] token = get_auth_token() headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" }

Great! We're connected. Let's test it out:

response = requests.get(f"{base_url}/user", headers=headers) print(response.json())

If you see your user info, you're golden!

Querying Data

Now for the fun part – let's grab some data:

query = { "model": "your_model", "view": "your_view", "fields": ["field1", "field2"], "limit": 100 } response = requests.post(f"{base_url}/queries", headers=headers, json=query) query_id = response.json()["id"] results = requests.get(f"{base_url}/query_results/{query_id}", headers=headers) print(results.json())

Boom! You've just run your first Looker query via API.

Working with Looks and Dashboards

Want to fetch data from an existing Look? Easy peasy:

look_id = 123 # Replace with your Look ID response = requests.get(f"{base_url}/looks/{look_id}/run/json", headers=headers) print(response.json())

For dashboards, you can grab the metadata like this:

dashboard_id = 456 # Replace with your Dashboard ID response = requests.get(f"{base_url}/dashboards/{dashboard_id}", headers=headers) print(response.json())

Manipulating User and Group Data

Need to create a new user? Here's how:

new_user = { "first_name": "Jane", "last_name": "Doe", "email": "[email protected]", "is_disabled": False } response = requests.post(f"{base_url}/users", headers=headers, json=new_user) print(response.json())

Adding a user to a group? No sweat:

user_id = 789 group_id = 101 response = requests.post(f"{base_url}/group_users", headers=headers, json={ "user_id": user_id, "group_id": group_id }) print(response.json())

Scheduling and Data Delivery

Let's set up a scheduled plan:

scheduled_plan = { "name": "Daily Report", "look_id": 123, "scheduled_plan_destination": [{ "format": "csv", "address": "[email protected]", "type": "email" }], "crontab": "0 9 * * *" } response = requests.post(f"{base_url}/scheduled_plans", headers=headers, json=scheduled_plan) print(response.json())

This will send a daily CSV report at 9 AM.

Error Handling and Best Practices

Always wrap your API calls in try-except blocks:

try: response = requests.get(f"{base_url}/user", headers=headers) response.raise_for_status() except requests.exceptions.RequestException as e: print(f"An error occurred: {e}")

And remember, Looker has rate limits. Be kind to the API – use pagination and avoid hammering it with requests.

Conclusion

And there you have it! You're now equipped to build some seriously cool Looker integrations. Remember, this is just scratching the surface. The Looker API is vast and powerful, so don't be afraid to explore and experiment.

For more advanced topics like streaming results or using the official Looker SDK, check out the Looker API Documentation.

Now go forth and build something awesome! Happy coding!