Share
Explore

Getting and Testing your GROK API

Start at

Windows Lab Instructions: Testing GROQ API with curl

This guide will walk you through using the curl command in a Windows environment to interact with the GROQ API.
Prerequisites:
Windows Operating System: Windows 10 or 11 is recommended.
curl installed: curl is usually pre-installed on modern Windows versions. You can verify by opening Command Prompt or PowerShell and typing curl --version. If it's not installed, you can download it from the official curl website or use a package manager like Chocolatey (choco install curl).
GROQ API Key: You will need an API key from GROQ. If you don't have one, sign up on the GROQ website and generate an API key. Keep your API key secure and do not share it publicly.
Steps:

1. Open Command Prompt or PowerShell

You can use either Command Prompt or PowerShell for these instructions. PowerShell offers a bit more flexibility, but curl works identically in both for basic API calls.
Command Prompt: Search for "cmd" in the Windows search bar and open "Command Prompt."
PowerShell: Search for "powershell" in the Windows search bar and open "Windows PowerShell."

2. Set Your GROQ API Key as an Environment Variable (Recommended)

To avoid directly embedding your API key in every curl command, it's good practice to set it as an environment variable.
In PowerShell:
PowerShell$env:GROQ_API_KEY = "YOUR_GROQ_API_KEY"
In Command Prompt:
DOSset GROQ_API_KEY="YOUR_GROQ_API_KEY"
Replace YOUR_GROQ_API_KEY with your actual GROQ API key.
Note: This sets the environment variable only for the current session. If you close the terminal, you'll need to set it again. For persistent environment variables, you'd typically add it via System Properties, but for lab testing, a session variable is sufficient.

3. Test a Basic GROQ API Call

Now, let's make a simple request to the GROQ API. We'll use the chat completions endpoint as an example.
Example: Chat Completions API Call
This example sends a request to the GROQ API to generate a chat completion.
Bashcurl -X POST https://api.groq.com/openai/v1/chat/completions \ -H "Authorization: Bearer %GROQ_API_KEY%" \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "Explain the concept of quantum entanglement in simple terms." } ], "model": "mixtral-8x7b-32768" }'
Explanation of the curl command:
-X POST: Specifies the HTTP request method as POST.
https://api.groq.com/openai/v1/chat/completions: This is the API endpoint for chat completions.
-H "Authorization: Bearer %GROQ_API_KEY%": Sets the Authorization header. %GROQ_API_KEY% (in Command Prompt) or $env:GROQ_API_KEY (in PowerShell) will be replaced by your API key.
-H "Content-Type: application/json": Specifies that the request body is in JSON format.
-d '{...}': Provides the request body (data) in JSON format.
"messages": An array of message objects.
"role": "user": Indicates the role of the message sender (user).
"content": "...": The actual message content.
"model": "mixtral-8x7b-32768": Specifies the GROQ model to use. You can find other available models in the GROQ API documentation.
If you are using PowerShell, you'll need to adjust the environment variable syntax:
PowerShellcurl -X POST https://api.groq.com/openai/v1/chat/completions ` -H "Authorization: Bearer $($env:GROQ_API_KEY)" ` -H "Content-Type: application/json" ` -d '{ "messages": [ { "role": "user", "content": "Explain the concept of quantum entanglement in simple terms." } ], "model": "mixtral-8x7b-32768" }'
Expected Output:
You should receive a JSON response from the GROQ API containing the generated completion. The exact content will vary, but it will look something like this (abbreviated for brevity):
JSON{ "id": "chatcmpl-...", "object": "chat.completion", "created": ..., "model": "mixtral-8x7b-32768", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Imagine you have two coins..." }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": ..., "completion_tokens": ..., "total_tokens": ... }, "system_fingerprint": "..." }

4. Experiment with Different Prompts and Models

You can modify the "content" of the user message to ask different questions or try different GROQ models (refer to the GROQ documentation for available models).
Example: Asking a different question
Bashcurl -X POST https://api.groq.com/openai/v1/chat/completions \ -H "Authorization: Bearer %GROQ_API_KEY%" \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "Write a short poem about a sunny day." } ], "model": "llama3-8b-8192" }'

5. (Optional) Troubleshooting

"curl: (6) Could not resolve host...": This usually means a problem with your internet connection or a typo in the API endpoint URL.
"Unauthorized" (HTTP 401): Your API key is incorrect, missing, or not properly passed in the Authorization header. Double-check your GROQ_API_KEY and the curl command syntax.
"Bad Request" (HTTP 400): There's an issue with your JSON request body. Check for typos, missing commas, or incorrect formatting.
curl not found: If curl --version didn't work, you need to install curl as mentioned in the prerequisites.
Quotes and Escaping: When using curl with JSON data in Command Prompt, pay close attention to double quotes and escaping. If you encounter issues, try enclosing the entire JSON string in single quotes (if your shell supports it) or carefully escaping internal double quotes with a backslash (\"). PowerShell is generally more forgiving with string handling.
This concludes the lab instructions for testing the GROQ API using the curl command on Windows. You can now use these examples as a basis for further exploration and integration with the GROQ API.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.