File size: 753 Bytes
4dbfff9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import axios from "axios";

export const apiClient = axios.create({
  baseURL: "/api",
});

apiClient.interceptors.request.use((config) => {
  // Only access localStorage in browser environment
  if (typeof window !== 'undefined') {
    const keysStr = localStorage.getItem("smartlead_api_keys");
    const activeId = localStorage.getItem("smartlead_active_key_id");

    if (keysStr && activeId) {
      try {
        const keys = JSON.parse(keysStr);
        const activeKey = keys.find((k: any) => k.id === activeId)?.key;
        if (activeKey) {
          config.headers["x-api-key"] = activeKey;
        }
      } catch (e) {
        console.error("Failed to parse API keys", e);
      }
    }
  }

  return config;
});