form-extractor-prototype icon indicating copy to clipboard operation
form-extractor-prototype copied to clipboard

If no API key configured, let users provide one

Open simonw opened this issue 10 months ago • 2 comments

A pattern I like for this is storing the user's API key in localstorage in their browser and sending it each time it's needed - but avoiding storing it on the server or in any log files anywhere, so I don't need to worry that a breach of my application could leak other people's API keys.

I built a simple tool that does that here: https://tools.simonwillison.net/haiku - code here: https://github.com/simonw/tools/blob/baebe5777294e4ecb5a482aca4919be214132024/haiku.html#L139

function getApiKey() {
  let apiKey = localStorage.getItem("ANTHROPIC_API_KEY");
  if (!apiKey) {
    apiKey = prompt("Please enter your Anthropic API key:");
    if (apiKey) {
      localStorage.setItem("ANTHROPIC_API_KEY", apiKey);
    }
  }
  return apiKey;
}

simonw avatar Apr 19 '24 15:04 simonw

Using localstorage could be a simple way to support persisting user's previous image uploads and form generations as well - then there's no need to store anything server-side.

simonw avatar Apr 19 '24 15:04 simonw

Lovely idea - thanks @simonw :-)

timpaul avatar Apr 23 '24 09:04 timpaul