0Pricing
Supabase Backend as a Service · Lezione

Distribuzione della prima funzione

Scriva e distribuisca una semplice Edge Function per gestire endpoint API personalizzati o la logica backend direttamente dal Suo progetto Supabase.

Distribuzione della prima funzione è una lezione Supabase Backend as a Service gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Supabase Backend as a Service, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Supabase Backend as a Service include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Making Your Code Go Live

Welcome to deploying your first Supabase Edge Function! This lesson will guide you through taking your function code and making it accessible via an API endpoint.

Deploying means publishing your function so it can be called from your app, a web browser, or other services. It's how your backend logic goes live!

The Supabase CLI

To deploy Edge Functions, you'll primarily use the Supabase Command Line Interface (CLI). This tool helps you manage your Supabase project locally and interact with your remote backend.

  • Ensure the CLI is installed and configured for your project.
  • You'll use commands like supabase functions new and supabase functions deploy.

Creating a New Function

Let's start by creating a new Edge Function. Navigate to your project's root directory in your terminal and use the supabase functions new command:

This command creates a new folder for your function with an index.ts file, which is where your Deno (TypeScript/JavaScript) code will live.

supabase functions new my-first-function

Function Code Structure

After creating the function, you'll find a new directory (e.g., supabase/functions/my-first-function) containing an index.ts file. This file is the entry point for your Edge Function.

Supabase Edge Functions are built on Deno, a secure runtime for JavaScript and TypeScript. They export a default function that handles incoming HTTP requests.

Writing a Simple Function

Now, let's write a basic 'Hello, World!' function. Open the index.ts file inside your new function's directory and replace its content with the following code:

This function simply returns a plain text response. It's a great starting point!

import { serve } from 'https://deno.land/std@0.177.0/http/server.ts'

serve(async (req) => {
  return new Response("Hello from your first Edge Function!", {
    headers: { 'Content-Type': 'text/plain' },
  })
})

Deploying Your Function

With your code ready, it's time to deploy! Use the supabase functions deploy command, specifying your function's name:

The CLI will build your function and upload it to your Supabase project. Once deployed, it becomes a live API endpoint!

supabase functions deploy my-first-function

Invoking with cURL

After deployment, you can test your function. The easiest way is using cURL in your terminal. You'll need your Supabase project URL and your project's Anon Key (found in your project settings).

Replace the placeholders with your actual project details:

curl -X POST 'https://your-project-ref.supabase.co/functions/v1/my-first-function' \
  -H 'Authorization: Bearer YOUR_ANON_KEY' \
  -H 'Content-Type: application/json' \
  -d '{}'

Invoking from Web (JS)

You can also call your Edge Function from any web application using standard fetch API. Here's how you might invoke it from client-side JavaScript:

Remember to replace the placeholder URL and key with your actual project details.

async function callMyFunction() {
  const SUPABASE_URL = "https://your-project-ref.supabase.co";
  const SUPABASE_ANON_KEY = "YOUR_ANON_KEY";

  try {
    const res = await fetch(
      `${SUPABASE_URL}/functions/v1/my-first-function`,
      {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${SUPABASE_ANON_KEY}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({})
      }
    )
    const data = await res.text() // Use .text() for plain text response
    console.log(data)
  } catch (error) {
    console.error("Error calling function:", error)
  }
}

callMyFunction()

Passing Data & Redeploying

Functions are more useful when they can accept input! Let's update our function to take a name from the request body and respond with a personalized greeting.

After modifying the index.ts file, you'll need to run supabase functions deploy my-first-function again to push the changes live.

import { serve } from 'https://deno.land/std@0.177.0/http/server.ts'

serve(async (req) => {
  const { name } = await req.json() // Expects { "name": "..." }
  const greeting = name ? `Hello, ${name}!` : "Hello there!"

  return new Response(
    JSON.stringify({ message: greeting }),
    { headers: { 'Content-Type': 'application/json' } },
  )
})

Quick Check: Deployment Steps

Which of the following is the correct sequence of steps to deploy a Supabase Edge Function after writing its code?

Recap: Your First Deployment!

You've successfully deployed your first Supabase Edge Function! Here's what we covered:

  • Used the Supabase CLI to manage functions.
  • Created a new function with supabase functions new.
  • Wrote a simple Deno function in index.ts.
  • Deployed it using supabase functions deploy.
  • Invoked the function using cURL and client-side JavaScript.
  • Learned to pass data to functions and redeploy changes.

Now you have a live backend endpoint ready for action!

Domande Frequenti

La lezione «Distribuzione della prima funzione» è gratuita?

Sì — il testo completo di «Distribuzione della prima funzione» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Supabase Backend as a Service, passa a CoddyKit PRO. Il corso Supabase Backend as a Service include 4 lezioni in totale.

Cosa imparerò in «Distribuzione della prima funzione»?

Scriva e distribuisca una semplice Edge Function per gestire endpoint API personalizzati o la logica backend direttamente dal Suo progetto Supabase. Eserciti Supabase Backend as a Service con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Supabase Backend as a Service?

Non è richiesta alcuna esperienza precedente. Supabase Backend as a Service su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Distribuzione della prima funzione»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Supabase Backend as a Service?

Sì. Ogni lezione Supabase Backend as a Service include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione alle Edge Functions
  2. Distribuzione della prima funzione
  3. Integrazione delle funzioni nell'applicazione
  4. Segreti, variabili d’ambiente e funzioni pianificate
← Torna a Supabase Backend as a Service