import { zodResolver } from '@hookform/resolvers/zod' import { parseAsBoolean, useQueryState } from 'nuqs' import { type SubmitHandler, useForm } 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_Shadcn_, FormControl_Shadcn_, FormField_Shadcn_, Input_Shadcn_, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import * as z from 'zod' import { useParams } from 'common' import { useAPIKeyCreateMutation } from 'data/api-keys/api-key-create-mutation' import { Plus, ShieldCheck } from 'lucide-react' 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(), }) const CreateSecretAPIKeyDialog = () => { const { ref: projectRef } = useParams() const [visible, setVisible] = useQueryState( 'new', parseAsBoolean.withDefault(false).withOptions({ history: 'push', clearOnDefault: true }) ) const onClose = (value: boolean) => { setVisible(value) } const form = useForm>({ resolver: zodResolver(SCHEMA), defaultValues: { name: '', description: '', }, }) 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.`) onClose(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.
) } export default CreateSecretAPIKeyDialog