Skip to content

Commit c81a096

Browse files
committed
Make the api_key constructor parameter Optional and read from env
1 parent 97c6c63 commit c81a096

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ async def main():
4040
asyncio.run(main())
4141
```
4242

43+
While you can provide an `api_key` argument, we recommend using `python-dotenv` to add `LMNT_API_KEY="My API Key"` to your `.env` file so that your API key is not stored in source control.
44+
4345
## More examples
4446

4547
You can find more examples in the [demo](demo) directory.

src/lmnt/api.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515

16-
import base64
16+
from aiohttp import WSMsgType
17+
from typing import List, Optional
18+
1719
import aiohttp
20+
import base64
1821
import json
1922
import os
20-
from aiohttp import WSMsgType
21-
from typing import List
23+
2224

2325
_BASE_URL = 'https://api.lmnt.com'
2426
_SYNTHESIZE_STREAMING_ENDPOINT = '/v1/ai/speech/stream'
@@ -121,11 +123,13 @@ async def finish(self):
121123

122124

123125
class Speech:
124-
def __init__(self, api_key: str, **kwargs):
126+
def __init__(self, api_key: Optional[str] = None, **kwargs):
125127
self._session = None
126-
self._api_key = api_key
128+
self._api_key = api_key or os.environ.get('LMNT_API_KEY')
127129
self._base_url = kwargs.get('base_url', _BASE_URL)
128130
self._connector = kwargs.get('connector', None)
131+
if not self._api_key:
132+
raise ValueError('Please set the `LMNT_API_KEY` environment variable or pass it in to the Speech constructor.')
129133

130134
async def __aenter__(self):
131135
self._lazy_init()

0 commit comments

Comments
 (0)