From ad753a69e74af86b7decfc85e0cb499041c68a6c Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Mon, 14 Jul 2025 23:44:40 -0800 Subject: [PATCH] DONE : Added Together AI API key validation --- app/UserAPIKey.tsx | 97 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 19 deletions(-) diff --git a/app/UserAPIKey.tsx b/app/UserAPIKey.tsx index 0d9739d..574a787 100644 --- a/app/UserAPIKey.tsx +++ b/app/UserAPIKey.tsx @@ -2,43 +2,102 @@ import { useEffect, useState } from "react"; +const isValidKeyFormat = (key: string) => /^[a-zA-Z0-9]{64,}$/.test(key); + export function UserAPIKey() { const [userAPIKey, setUserAPIKey] = useState(() => { - // Only run in browser if (typeof window !== "undefined") { return localStorage.getItem("togetherApiKey") || ""; } return ""; }); + const [isValid, setIsValid] = useState(true); + const [isChecking, setIsChecking] = useState(false); + + const validateKey = async (key: string) => { + if (!key) { + setIsValid(true); + return; + } + + if (!isValidKeyFormat(key)) { + setIsValid(false); + return; + } + + setIsChecking(true); + + try { + const res = await fetch("https://api.together.xyz/v1/models", { + headers: { + Authorization: `Bearer ${key}`, + }, + }); + + setIsValid(res.ok); + } catch { + setIsValid(false); + } finally { + setIsChecking(false); + } + }; + useEffect(() => { if (userAPIKey) { localStorage.setItem("togetherApiKey", userAPIKey); + validateKey(userAPIKey); } else { localStorage.removeItem("togetherApiKey"); + setIsValid(true); } }, [userAPIKey]); return ( -
-
-

[Optional] Add your

- - Together API Key: - +
+
+
+

[Optional] Add your

+ + Together API Key: + +
+ setUserAPIKey(e.target.value)} + placeholder="API key" + className={`h-8 rounded border-[0.5px] px-2 text-sm focus-visible:outline + ${ + isValid + ? "border-gray-700 bg-gray-900 focus-visible:outline-gray-200" + : "border-red-500 bg-red-900 focus-visible:outline-red-300" + }`} + />
- setUserAPIKey(e.target.value)} - placeholder="API key" - className="h-8 rounded border-[0.5px] border-gray-700 bg-gray-900 px-2 text-sm focus-visible:outline focus-visible:outline-gray-200" - /> + + {!isChecking && !isValid && ( +

+ Invalid Together AI key. Please double-check or create one at{" "} + + Together API settings + . +

+ )} + + {isChecking && ( +

Validating key...

+ )}
); }