Integrating an Uncensored Text API with Python
Developers often seek a straightforward way to connect their applications to an unfiltered language model for consistent text generation. This guide walks you through the process of integrating our API using Python, ensuring you can handle text input and output reliably. We cover SDK settings, error handling, and a practical checklist to verify your setup works as expected.
Setting Up Your Python Environment
Before you can interact with the API, you need to prepare your development environment. The most common approach is to use a virtual environment to isolate your project dependencies. This ensures that the libraries you install do not conflict with other projects on your machine. You can create a virtual environment using tools like venv or poetry, depending on your preference.
Once your environment is active, you will need to install an HTTP client library. The requests library is a popular choice for making HTTP calls in Python. It provides a simple and intuitive interface for sending requests and handling responses. You can install it using pip by running the command pip install requests in your terminal.
Additionally, you may want to install python-dotenv to manage your API keys securely. This library allows you to store sensitive information in a .env file, which you can then load into your Python script. This practice helps keep your API key out of your version control system, reducing the risk of accidental exposure.
Finally, ensure that your Python version is compatible with the libraries you are using. Most modern HTTP clients support Python 3.7 and above. Check your Python version by running python --version in your terminal. If you need to upgrade, you can download the latest version from the official Python website.
Authentication and API Key Management
Authentication is a critical step in integrating with the API. Our service uses API keys to identify your account and track usage. When you sign up, you receive an API key immediately, which you can use to make requests. It is important to keep this key secure, as anyone with the key can use your account and incur charges.
To use the API key, you need to include it in the headers of your HTTP requests. Specifically, you should set the Authorization header to Bearer YOUR_API_KEY. This tells the server that you are authenticated and authorized to access the resources. Make sure to replace YOUR_API_KEY with the actual key you received during signup.
When managing your API key, consider using environment variables to store it. This prevents the key from being hardcoded in your script, which can be a security risk. You can load the key from an environment variable using the os module or a library like python-dotenv. This approach makes it easy to switch between different keys or environments without changing your code.
If you lose your API key, you can generate a new one from your account dashboard. The old key will remain valid until you revoke it, so you can switch to the new key without interrupting your service. Always revoke old keys when you no longer need them to maintain security.
Making Your First API Request
Now that your environment is set up and your API key is secured, you can make your first request to the API. The base URL for the API is https://api.uncensoredllm.cc/v1. You will be interacting with the /v1/chat/completions endpoint, which accepts POST requests. This endpoint allows you to send text input and receive text output.
To make a request, you need to construct a JSON payload that includes the model ID, messages, and any optional parameters. The model ID for our uncensored model is uncensored. The messages parameter should be a list of dictionaries, each representing a message in the conversation. Each message must have a role (either user or assistant) and a content field.
Here is an example of a simple request payload: {"model": "uncensored", "messages": [{"role": "user", "content": "Hello, how are you?"}]}. You can send this payload using the requests.post method, passing the base URL, headers, and JSON data. The response will contain the model's reply, which you can extract and display.
It is important to handle the response properly. Check the status code to ensure the request was successful. A status code of 200 indicates success. If you receive a 401 error, your API key is invalid. A 402 error means you have no credit, and a 429 error indicates you have hit the rate limit. Always check these codes to troubleshoot any issues.
Handling Streaming Responses
One of the powerful features of our API is streaming. This allows you to receive the model's response in chunks as it is generated, rather than waiting for the entire response to complete. This is particularly useful for long responses or real-time applications where you want to display text as it is being generated.
To enable streaming, you need to set the stream parameter to true in your request payload. When you make a streaming request, the server will send a series of Server-Sent Events (SSE) containing partial responses. You can process these events as they arrive, updating your application's interface in real time.
When using streaming, the usage information (token counts) is included in the last chunk of the response. This means you need to wait for the final chunk to get the total token usage for billing purposes. Make sure to handle the end of the stream properly to avoid hanging your application.
Streaming can also be combined with other parameters like temperature and top_p to control the randomness and diversity of the output. Experiment with these parameters to find the best settings for your specific use case. Remember that streaming adds complexity to your code, so weigh the benefits against the implementation effort.
Function Calling and JSON Mode
Our API supports function calling, which allows you to define tools that the model can use to perform specific actions. This is useful for applications that need to interact with external systems or perform complex tasks. You can define functions in your request payload, and the model will return a structured response indicating which function to call and with what arguments.
Additionally, you can use JSON mode to force the model to output its response in a strict JSON format. This is helpful when you need to parse the response programmatically. To enable JSON mode, set the response_format parameter to {"type": "json_object"}. The model will then output a valid JSON object, which you can easily parse using Python's json module.
When using function calling, make sure to handle the model's response carefully. The model may not always call the correct function or provide the correct arguments. You should validate the response before executing the function to avoid errors. This adds a layer of reliability to your application, ensuring that the model's actions are safe and correct.
Combining function calling with JSON mode can be powerful for building complex applications. For example, you can use function calling to trigger a database query and JSON mode to format the results. This allows you to create dynamic and responsive applications that can adapt to different inputs and outputs.
When This API Is Not the Right Choice
While our API is excellent for uncensored text generation, it may not be the right choice for every use case. For instance, if you need image, audio, or video generation, our API does not support these capabilities. You would need to look for a different provider that offers multimodal outputs.
Similarly, if you require embeddings for semantic search or fine-tuning for custom model training, our API does not provide these features. Our service is focused on text-in, text-out functionality. If these features are critical to your project, you should consider other options.
Additionally, if you need a service with a strict Service Level Agreement (SLA) or certifications like SOC2 or HIPAA, our API may not meet those requirements. We do not claim these certifications, so if compliance is a major factor for your organization, you should verify the provider's credentials carefully.
Finally, if you are looking for a free unlimited tier, our API is not the best fit. We offer a prepaid credit system with no monthly fee, but you need to purchase credit to use the service. If you need a free tier with generous limits, you might want to explore other providers. Always evaluate your specific needs before choosing a service.
Testing and Troubleshooting Checklist
To ensure your integration is working correctly, you should perform a series of tests. Start by verifying that your API key is valid by making a simple request. If you receive a 200 status code, your key is working. If you get a 401 error, check that you are using the correct key and that it has not been revoked.
Next, test the streaming functionality by making a request with the stream parameter set to true. Ensure that you are receiving chunks of data and that the final chunk contains the usage information. If the stream hangs, check your code for proper error handling and stream termination.
Test function calling by defining a simple function and sending a request that triggers it. Verify that the model returns the correct function name and arguments. If the model fails to call the function, check your function definitions and ensure they are correctly formatted in the request payload.
Finally, test error handling by intentionally triggering errors, such as sending a request with an invalid model ID or exceeding the rate limit. Ensure that your application handles these errors gracefully and provides meaningful feedback to the user. This checklist helps you identify and resolve issues before they affect your users.
Frequently asked questions
How do I get an API key for the uncensored LLM API?
You can get an API key by signing up with Google or email and password on our website. The key is shown immediately after signup, so you can start using it right away without any additional steps.
What is the cost of using the uncensored LLM API?
The API charges $0.15 per 1 million input tokens and $0.50 per 1 million output tokens. You pay for prepaid credit, which never expires, and there are no monthly fees or subscriptions. Errors and refusals are free.
Does the API support streaming responses?
Yes, the API supports streaming over Server-Sent Events (SSE). You can enable this by setting the stream parameter to true in your request. The usage information is included in the last chunk of the response.
What happens if I run out of credit?
If you run out of credit, you will receive a 402 error. You can top up your credit using crypto, specifically USDT on TRC20 or USDC on Base. The minimum top-up amount is $20, and the maximum is $500.
More guides
- Best LLM API for Creative Writing and Fiction
- LLM API Function Calling: A Working Example
- Uncensored LLM Benchmark: Test a Model Yourself
- Uncensored LLM 2025: Hosted vs Local Options
- Best Uncensored LLM: Myths vs Facts
- Estimating LLM API Costs for Your Project
- Troubleshooting Filter Issues in API Responses