import { zodResolver } from '@hookform/resolvers/zod' import { useParams } from 'common' import { Plus, ShieldCheck } from 'lucide-react' import { parseAsString, useQueryState } from 'nuqs' import { useForm, type SubmitHandler } from 'react-hook-form' import { toast } from 'sonner' import { Alert_Shadcn_, AlertDescription_Shadcn_, AlertTitle_Shadcn_, Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, DialogTrigger, Form, FormControl, FormField, Input_Shadcn_, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import * as z from 'zod' import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation' const NAME_SCHEMA = z .string() .min(4, 'Name must be at least 4 characters') .max(64, "Name can't be more than 64 characters long") .regex(/^[a-z0-9_]+$/, 'Name can only contain lowercased letters, digits and underscore') .refine((val: string) => !val.match(/^[0-9].+$/), 'Name must not start with a digit') .refine( (val: string) => val !== 'anon' && val !== 'service_role', 'Using "anon" or "service_role" for API key name is not possible' ) const FORM_ID = 'create-secret-api-key' const SCHEMA = z.object({ name: NAME_SCHEMA, description: z.string().max(256, "Description shouldn't be too long").trim(), }) export const CreateSecretAPIKeyDialog = () => { const { ref: projectRef } = useParams() const [visible, setVisible] = useQueryState('new', parseAsString) const onOpenChange = (value: boolean) => { if (value) setVisible('secret') else setVisible('') } const defaultValues = { name: '', description: '' } const form = useForm>({ resolver: zodResolver(SCHEMA), defaultValues, }) const { mutate: createAPIKey, isPending: isCreatingAPIKey } = useAPIKeyCreateMutation() const onSubmit: SubmitHandler> = async (values) => { createAPIKey( { projectRef, type: 'secret', name: values.name, description: values.description, }, { onSuccess: (data) => { toast.success(`Your secret API key ${data.prefix}... is ready.`) form.reset(defaultValues) onOpenChange(false) }, } ) } return ( Create new secret API key

Secret API keys allow elevated access to your project's data, bypassing Row-Level security.

( )} /> ( )} /> Securing your API key
  • Keep this key secret.
  • Do not use on the web, in mobile or desktop apps.
  • Don't post it publicly or commit in source control.
  • This key provides elevated access to your data, bypassing Row-Level Security.
  • If it leaks or is revealed, swap it with a new secret API key and then delete it.
  • If used in a browser, it will always return HTTP 401 Unauthorized. Delete immediately.
) }