#in an expo app with clerk, how do i send the token to a convex http endpoint?

4 messages · Page 1 of 1 (latest)

cyan ibex
#

@warm dome

warm domeBOT
#

Hi @cyan ibex. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To send a Clerk authentication token to a Convex HTTP endpoint in an Expo app, you need to:

  1. Get the token from Clerk
  2. Include it in the Authorization header when making requests to your Convex HTTP endpoint

Here's how to implement this:

// Get the token from Clerk
import { useAuth } from "@clerk/clerk-expo";

function YourComponent() {
  const { getToken } = useAuth();
  
  const callConvexHttpEndpoint = async () => {
    // Get the token with the "convex" template
    const token = await getToken({ template: "convex" });
    
    // Make the request to your Convex HTTP endpoint
    const response = await fetch(`${CONVEX_URL}/your-endpoint`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${token}`
      },
      body: JSON.stringify({
        // Your request data
      })
    });
    
    // Handle the response
    const data = await response.json();
    console.log(data);
  };
  
  // Rest of your component
}

It's important to use the correct token by specifying the template: "convex" parameter when calling getToken() User Authentication with Clerk and Convex. This ensures you're using the JWT template you configured in the Clerk dashboard specifically for Convex.

On the Convex side, your HTTP action can access the authenticated user's identity with ctx.auth.getUserIdentity() Debugging Authentication.

#

Remember that you need to have properly configured the Clerk JWT template for Convex in your Clerk dashboard and set up the authentication configuration in your Convex project for this to work correctly.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: