55"""
66
77import logging
8+ import os
89from datetime import datetime , timedelta
910from typing import Any
1011
@@ -64,7 +65,7 @@ def __init__(
6465 self ._load_stored_credentials ()
6566
6667 def _load_stored_credentials (self ) -> None :
67- """Load credentials from secure storage."""
68+ """Load credentials from secure storage or environment variable ."""
6869 credentials = self .storage .retrieve_credentials ()
6970 if credentials :
7071 self ._access_token = credentials .get ("access_token" )
@@ -75,6 +76,18 @@ def _load_stored_credentials(self) -> None:
7576 self ._token_expires_at = datetime .fromisoformat (expires_at )
7677
7778 logger .debug ("Loaded stored credentials" )
79+ else :
80+ # Check for API key in environment variable
81+ api_key = os .getenv ("LINEAR_API_KEY" )
82+ if api_key :
83+ # Validate the API key before using it
84+ if self ._validate_api_key (api_key ):
85+ self ._access_token = api_key
86+ self ._refresh_token = None
87+ self ._token_expires_at = None # API keys don't expire
88+ logger .info ("Loaded API key from LINEAR_API_KEY environment variable" )
89+ else :
90+ logger .warning ("Invalid API key found in LINEAR_API_KEY environment variable" )
7891
7992 def _save_credentials (self ) -> None :
8093 """Save credentials to secure storage."""
@@ -161,7 +174,7 @@ def _validate_api_key(self, api_key: str) -> bool:
161174 # Validate by calling viewer query - minimal API call that confirms both authentication and basic API access
162175 query = "query { viewer { id name } }"
163176 headers = {
164- "Authorization" : f"Bearer { api_key } " ,
177+ "Authorization" : api_key ,
165178 "Content-Type" : "application/json" ,
166179 }
167180 data = {"query" : query }
0 commit comments