// Package explee reads outreach campaigns and their results from // explee.com's public API (https://api.explee.com/public/api/docs). It only // reads: Supermarket never starts, changes or sends anything there. package explee import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "strconv" "strings" "time" ) const apiBase = "https://api.explee.com/public/api/v1" // ErrUnauthorized means the key was refused. var ErrUnauthorized = errors.New("explee.com refused the API key") // Client calls the API with one key. type Client struct { key, base string http *http.Client } func New(key string) *Client { return &Client{key: key, base: apiBase, http: &http.Client{Timeout: 60 * time.Second}} } // WithBase points the client at another server (for tests). func (c *Client) WithBase(u string) *Client { c.base = strings.TrimRight(u, "/") return c } func (c *Client) get(ctx context.Context, path string, q url.Values, out any) error { u := c.base + path if len(q) > 0 { u += "?" + q.Encode() } return c.do(ctx, http.MethodGet, u, nil, out) } func (c *Client) post(ctx context.Context, path string, body, out any) error { raw, err := json.Marshal(body) if err != nil { return err } return c.do(ctx, http.MethodPost, c.base+path, raw, out) } func (c *Client) do(ctx context.Context, method, u string, body []byte, out any) error { path := strings.TrimPrefix(u, c.base) var rd io.Reader if body != nil { rd = bytes.NewReader(body) } req, err := http.NewRequestWithContext(ctx, method, u, rd) if err != nil { return err } if body != nil { req.Header.Set("Content-Type", "application/json") } req.Header.Set("X-API-Key", c.key) req.Header.Set("Accept", "application/json") res, err := c.http.Do(req) if err != nil { return err } defer res.Body.Close() raw, err := io.ReadAll(io.LimitReader(res.Body, 32<<20)) if err != nil { return err } if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden { return ErrUnauthorized } if res.StatusCode == http.StatusPaymentRequired { return errors.New("explee.com needs a positive credit balance for searches (HTTP 402)") } if res.StatusCode != http.StatusOK { return fmt.Errorf("explee.com %s: HTTP %d: %s", path, res.StatusCode, strings.TrimSpace(string(raw[:min(len(raw), 300)]))) } return json.Unmarshal(raw, out) } // Me checks the key; it returns the key owner's email or name. func (c *Client) Me(ctx context.Context) (string, error) { var out map[string]any if err := c.get(ctx, "/me", nil, &out); err != nil { return "", err } for _, k := range []string{"email", "name", "organization_name"} { if v, ok := out[k].(string); ok && v != "" { return v, nil } } return "", nil } // Campaign is an outreach campaign. type Campaign struct { ID int64 `json:"id"` ProjectID int64 `json:"project_id"` Name string `json:"name"` TargetURL string `json:"target_url"` // the booking link Status string `json:"status"` } // Campaigns lists every campaign, archived ones too. func (c *Client) Campaigns(ctx context.Context) ([]Campaign, error) { var out struct { Campaigns []Campaign `json:"campaigns"` } err := c.get(ctx, "/autogtm/campaigns", url.Values{"include_archived": {"true"}}, &out) return out.Campaigns, err } // Analytics are a campaign's lifetime numbers. type Analytics struct { Status string `json:"status"` EmailsSent int `json:"emails_sent"` TotalReplies int `json:"total_replies"` ReplyRatePct float64 `json:"reply_rate_pct"` HotLeads int `json:"hot_leads"` SpendUSD float64 `json:"spend_usd"` CostPerLeadUSD float64 `json:"cost_per_lead_usd"` LeadsPoolUsed int `json:"leads_pool_used"` ManualStatus map[string]int `json:"manual_status_counts"` ColdLost int `json:"cold_lost"` } func (c *Client) Analytics(ctx context.Context, campaignID int64) (*Analytics, error) { var out Analytics err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/analytics", url.Values{"period": {"all"}}, &out) return &out, err } // Lead is a contacted lead. type Lead struct { PersonID string `json:"person_id"` CampaignID int64 `json:"campaign_id"` Erased bool `json:"erased"` // they asked to be removed: no personal data FirstName string `json:"first_name"` LastName string `json:"last_name"` Name string `json:"name"` Email string `json:"email"` JobTitle string `json:"job_title"` LinkedInURL string `json:"linkedin_url"` CompanyName string `json:"company_name"` CompanyDomain string `json:"company_domain"` FirstSentAt string `json:"first_sent_at"` Status string `json:"status"` // set by the team in the inbox, e.g. Hot, Won Replied bool `json:"replied"` Bounced bool `json:"bounced"` Unsubscribed bool `json:"unsubscribed"` LastReplyAt string `json:"last_reply_at"` } // Leads lists every contacted lead of a campaign. func (c *Client) Leads(ctx context.Context, campaignID int64) ([]Lead, error) { var all []Lead offset := 0 for page := 0; page < 200; page++ { var out struct { Leads []Lead `json:"leads"` HasMore bool `json:"has_more"` NextOffset *int `json:"next_offset"` } q := url.Values{"limit": {"1000"}, "offset": {strconv.Itoa(offset)}} if err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/leads", q, &out); err != nil { return all, err } all = append(all, out.Leads...) if !out.HasMore || out.NextOffset == nil { break } offset = *out.NextOffset } return all, nil } // HotLead is a lead whose reply showed interest. type HotLead struct { PersonID string `json:"person_id"` CampaignID int64 `json:"campaign_id"` Erased bool `json:"erased"` Name string `json:"name"` Email string `json:"email"` JobTitle string `json:"job_title"` CompanyName string `json:"company_name"` CompanyDomain string `json:"company_domain"` LinkedInURL string `json:"linkedin_url"` Phone string `json:"phone"` WhyHot string `json:"why_hot"` BecameHotAt string `json:"became_hot_at"` } // HotLeads lists hot leads that became hot at or after since (zero: all). func (c *Client) HotLeads(ctx context.Context, since time.Time) ([]HotLead, error) { var all []HotLead offset := 0 for page := 0; page < 200; page++ { var out struct { Leads []HotLead `json:"leads"` HasMore bool `json:"has_more"` NextOffset *int `json:"next_offset"` } q := url.Values{"limit": {"200"}, "offset": {strconv.Itoa(offset)}} // the API allows at most 200 if !since.IsZero() { q.Set("since", since.UTC().Format(time.RFC3339)) } if err := c.get(ctx, "/autogtm/hot-leads", q, &out); err != nil { return all, err } all = append(all, out.Leads...) if !out.HasMore || out.NextOffset == nil { break } offset = *out.NextOffset } return all, nil } // CompanySearch asks for companies that do something, optionally scored // against criteria. The first 10 results of a search are free. type CompanySearch struct { Definition string // what the companies do, in plain words Criteria []string // each company is scored 0-5 against these SizeMin int // employees; 0: no limit SizeMax int PageSize int // 10 stays in the free results } // Company is one search result. type Company struct { Name string `json:"name"` Domain string `json:"domain"` URL string `json:"url"` Description string `json:"description"` Industry string `json:"industry"` Geo string `json:"geo"` GeoCity string `json:"geo_city"` Size *int `json:"size"` LinkedInID *int64 `json:"linkedin_id"` Criteria map[string]struct { Score float64 `json:"score"` Reasoning string `json:"reasoning"` } `json:"criteria"` Match *int `json:"match"` // 1 no match, 2 weak, 3 strong (with criteria) } // SearchCompanies runs a company search. func (c *Client) SearchCompanies(ctx context.Context, s CompanySearch) ([]Company, error) { filters := map[string]any{"definition": s.Definition} if len(s.Criteria) > 0 { filters["criteria"] = s.Criteria } if s.SizeMin > 0 || s.SizeMax > 0 { size := map[string]int{} if s.SizeMin > 0 { size["min"] = s.SizeMin } if s.SizeMax > 0 { size["max"] = s.SizeMax } filters["size"] = size } if s.PageSize <= 0 { s.PageSize = 10 } var out struct { Companies []Company `json:"companies"` } err := c.post(ctx, "/search/companies", map[string]any{"filters": filters, "page": 1, "page_size": s.PageSize}, &out) return out.Companies, err }