Back

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

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 powerhouse that lets you tap into your data like never before. Whether you're looking to pull data, manage users, or embed Looker content, this guide will get you up and running in no time.

Prerequisites

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

  • Access to a Looker instance (duh!)
  • API3 credentials (your golden ticket)
  • A Node.js environment set up and ready to roll

Got all that? Great! Let's get our hands dirty.

Setting up the project

First things first, let's get our project off the ground:

mkdir looker-api-integration cd looker-api-integration npm init -y npm install @looker/sdk

Boom! You're all set up. Easy peasy, right?

Authentication

Now for the fun part - authentication. Looker uses OAuth2, so let's implement that flow:

const { LookerNodeSDK } = require('@looker/sdk'); const sdk = LookerNodeSDK.init40(); async function authenticate() { try { await sdk.ok(sdk.me()); console.log("Authentication successful!"); } catch (error) { console.error("Authentication failed:", error); } } authenticate();

Pro tip: Store those access tokens securely. Your future self will thank you.

Making API requests

With authentication out of the way, let's make some requests:

async function runQuery() { try { const result = await sdk.ok(sdk.run_inline_query({ result_format: "json", body: { model: "your_model", view: "your_view", fields: ["field1", "field2"], limit: 10 } })); console.log(result); } catch (error) { console.error("Query failed:", error); } }

See how easy that was? You're now pulling data like a pro!

Key API endpoints

Looker's API is vast, but here are some endpoints you'll probably use a lot:

  • /queries for running queries
  • /looks for managing looks
  • /dashboards for working with dashboards
  • /users for user management

Explore these, and you'll be a Looker API wizard in no time.

Implementing common use cases

Let's tackle a common scenario - embedding Looker content:

async function getEmbedUrl() { const user = await sdk.ok(sdk.me()); const url = await sdk.ok(sdk.create_embed_url_as_me({ target_url: "https://your-looker-instance.com/embed/dashboards/123", session_length: 3600, force_logout_login: true })); console.log("Embed URL:", url); }

Now you can embed Looker dashboards in your app. How cool is that?

Best practices

A few things to keep in mind:

  • Respect rate limits. Looker's not a fan of spam.
  • Implement pagination for large datasets.
  • Log errors and handle them gracefully.
  • Keep your credentials secret. Seriously.

Testing and debugging

Testing is crucial. Here's a quick example using Jest:

test('authenticate successfully', async () => { const user = await sdk.ok(sdk.me()); expect(user).toBeDefined(); });

When debugging, the Looker API explorer is your best friend. Use it liberally!

Conclusion

And there you have it! You're now equipped to build awesome Looker API integrations. Remember, the Looker documentation is extensive and super helpful, so don't hesitate to dive deeper.

Now go forth and build something amazing. You've got this!