Pack API Object Return/Schema advice

Good morning everyone! I am very close to having my Hunter.io ready to go! WAHOO!

The pack will find the most likely email for a prospect given their first and last name and the domain of their employer.

Here is the API - Hunter's API Reference V2.

The GET request returns a good chunk of data but I am really only interested in the “email” and the “score.” I am currently just returning the response as a string but it is time to get into Schemas/Objects in Coda!

I have defined the Schema as this:

const EmailSchema = coda.makeObjectSchema({
  properties: {
 
    email: {
      description: "Most likely email",
      type: coda.ValueType.String,
      codaType: coda.ValueHintType.Email,
    },
    score: {
      description: "How certain we are.",
      type: coda.ValueType.String,
      
    },
  },
 
  displayProperty: "email",
});

I currently have the response formatted like this:

 execute: async function ([first_name, last_name, domain,api_key], context) {
    let url = coda.withQueryParams("https://api.hunter.io/v2/email-finder", {
  domain: domain,
  first_name: first_name,
  last_name: last_name,
  api_key: api_key,

});
let response = await context.fetcher.fetch({
  method: "GET",
  url: url,
});

    return response.body;
  },

  resultType: coda.ValueType.String
});

My question is what is the most elegant way to only return both the “email” and “score” value?

2 Likes

Here was my first crack at it:

let data = response.body.data

return {
  email:data.email,
  score: data.score,
};
  },
});
1 Like

Hey there!

Since you are defining a schema, you don’t want your result type to be a string - you want it to be an Object.

In Coda those objects visually represent as chips and their various aspects can be drawn out on the Coda side using chaining.

So I would have the resultType be an object instead that holds:

  • likely email
  • score

Also - you don’t want to keep your api key as a Parameter for the function, but rather as part of the Auth flow in pack.SetUserAuthentication()

That way the user won’t have to sign in with their api key everywhere they use the formula.

I’m sure @Eric_Koleda amongst others can add a lot more!

3 Likes

Thanks, Scott!

I corrected the resultType: coda.ValueType.Object after I posted this morning which definitely got me a lot further than returning a string. I now get the “likely email” and can hover over to get the score.

I am using the System Authentication like this so I believe it is pulling from there on the function but definitely double check me!

pack.setSystemAuthentication({
  type: coda.AuthenticationType.QueryParamToken,
  paramName: "api_key",
});
1 Like

@Thomas_Baucom - Great work so far! To address a few questions:

My question is what is the most elegant way to only return both the “email” and “score” value?

You actually don’t need to! To quote the documentation:

When a formula or sync table returns an object, only the fields matching the properties defined in the schema are retained, and all others are discarded.

It will work as long as the keys in the response match the keys of the properties. Any extra data in the response will be ignored.

I am using the System Authentication like this so I believe it is pulling from there on the function but definitely double check me!

Yep, that looks great! I think @Scott_Collier-Weir was noticing you also seem to be passing the API key as a parameter:

 execute: async function ([first_name, last_name, domain, api_key], context) {

If you got it setup using system auth then there is no need to pass it as a parameter as well.

1 Like

Thanks for the response! I learned a lot building this one. Special thanks to you and @Scott_Collier-Weir for the help and inspiration.

This pack is now doing exactly what I need it to inside the doc! This community is just the best.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.