EN ▾
https://api.sdxlnsfw.cc/v1$0.15 per 1M input tokens$0.50 Output tokens / 1M100,000 token context
Get API key

Integrating SDXL NSFW API in Python

Developers often seek a reliable way to connect to an uncensored text model using standard tools. This guide walks you through connecting to our API in Python, covering authentication, SDK configuration, and sending your first request.

Python developer workspace with API integration illustration

Setting Up Your Python Environment

Before writing any code, you need to install the required libraries. The most common approach is to use the official OpenAI Python SDK, as our API follows the same structure. This allows you to use familiar methods and objects without learning a new protocol from scratch. The SDK handles the heavy lifting of HTTP requests and JSON parsing, letting you focus on application logic rather than network protocols.

Open your terminal or command prompt and run the installation command. You should use pip to install the package in your project directory. This ensures that your virtual environment remains clean and isolated from other projects. A clean environment prevents dependency conflicts and makes deployment predictable across different machines.

  • Run pip install openai to install the SDK.
  • Ensure you are using a recent version of Python, preferably 3.8 or higher.
  • Create a virtual environment to manage dependencies effectively.

Once installed, you can import the necessary classes in your Python script. The SDK handles the HTTP requests, JSON serialization, and response parsing for you. This reduces the amount of boilerplate code you need to write manually. You will need to import the OpenAI client class to initialize the connection to our service.

Authentication and API Key Management

Authentication is handled via an API key that you receive after signing up. You can sign up using Google or an email and password combination. The key is displayed immediately upon creation, so save it securely. Since the key grants access to your prepaid credit, treat it like a password. Losing it means losing access to your funds until you recover the account.

Pass this key to the SDK by setting the api_key parameter. You should not hardcode this value in your source files if you plan to share your code. Instead, use environment variables or a configuration file. This practice prevents accidental exposure of your credentials in version control systems like GitHub.

The base URL for our service is https://api.sdxlnsfw.cc/v1. You must configure the SDK to point to this URL instead of the default OpenAI endpoint. This ensures that your requests reach our uncensored model instead of a standard commercial model. The base URL defines the server that processes your tokens and returns the text.

Keep your key private. If you suspect it has been compromised, you can regenerate it from your dashboard. The SDK will use this key for all subsequent requests until you update it. Regular rotation of keys is a good security practice for any API integration.

Configuring the Client Object

After installing the SDK and obtaining your key, you need to initialize the client object. This object manages the connection settings and authentication for all API calls. It is the entry point for sending requests to our service. The client object holds your configuration and reuses connections for efficiency.

Instantiate the client with your API key and the correct base URL. This configuration tells the SDK where to send your data. It also sets up the default headers and timeout values. Without the correct base URL, the SDK might send data to the wrong server, resulting in errors or unexpected responses.

Here is how you create the client in Python. You pass the key and the base URL as arguments. This setup is minimal and straightforward, allowing you to focus on the logic of your application. The client object remains active throughout your script's execution.

  • Create the client instance with your credentials.
  • Verify the connection by listing models.
  • Ensure the base URL matches our service endpoint.

This step is important for ensuring that your requests are routed correctly. The client object abstracts the network layer, allowing you to call methods like chat.completions.create with simple Python code. It handles retries and error codes automatically.

Sending a Chat Completion Request

Our API supports the /v1/chat/completions endpoint. This endpoint accepts a list of messages and returns a generated response. The model ID you must use is uncensored. This specifies that you want the uncensored open-weight model. The model ID is a string that identifies the specific neural network weights being used for generation.

Construct a message list with a role and content. The role can be system, user, or assistant. The content is the text you want to send. The API processes this input and generates a text response. The system role sets the behavior, while the user role provides the prompt. The assistant role is used for few-shot examples.

Call the chat.completions.create method on your client object. Pass the model ID, the message list, and any optional parameters like temperature or top_p. The SDK handles the HTTP POST request and returns a response object. You can also enable streaming for real-time output.

Extract the generated text from the response object. You can then use this text in your application. The response includes usage statistics, which you can log for monitoring purposes. These statistics help you track token consumption and manage your prepaid credit effectively.

Handling Errors and Rate Limits

Errors are an inevitable part of API integration. Our API returns specific HTTP status codes to indicate the nature of the error. Understanding these codes helps you debug issues quickly. You should implement try-except blocks to catch these errors gracefully. This prevents your application from crashing unexpectedly.

A 401 error indicates an invalid or missing API key. A 402 error means you have no credit left. A 429 error indicates that you have exceeded the rate limit of 300 requests per minute. Rate limits are enforced per key, so high-volume applications need to manage their request frequency carefully.

Implement error handling in your code to catch these exceptions. You can retry requests after a short delay for rate limit errors. For authentication errors, check your key configuration. For insufficient credit errors, you need to top up your account with crypto. The API does not charge for errors, so you can retry without penalty.

Errors and refusals are free of charge. This means you are not billed for failed requests or content refusals. This is a significant advantage for testing and development. You can experiment with different prompts and parameters without worrying about wasting credits on failed attempts.

When This API Is Not the Right Choice

While our API is excellent for text generation, it has specific limitations. It does not support image, audio, or video generation. If you need to generate media, you will need a different service. The API is text-in and text-out only, so complex multimodal tasks require external tools.

The API also lacks embeddings, OCR, and fine-tuning capabilities. If your application requires vector search or document scanning, this API will not meet your needs. You would need to combine it with other tools for those features. For example, you might use a separate embedding service to convert text into vectors for search.

Additionally, the model is text-only. It does not support function calling for complex tool use, although it does support basic function calling. If you need advanced tool integration, you might need a more feature-rich model. Basic function calling allows you to define tools, but advanced routing and multi-step tool use may require a different provider.

Finally, if you require a Service Level Agreement (SLA) or specific certifications like SOC2, this service may not be suitable. It is an independent service without those guarantees. Check the documentation for current limits and features before committing. This API is ideal for developers who need raw text generation without enterprise bells and whistles.

Addressing Common Developer Questions

Developers frequently ask how to handle streaming responses and manage token usage. Streaming allows you to receive text in chunks as it is generated, which improves the user experience for long responses. It reduces the perceived latency for the end user. The API supports streaming over Server-Sent Events (SSE). You can enable this by setting the stream parameter to true. The usage information is included in the last chunk of the response.

Another common question is about pricing. The cost is $0.15 per 1 million input tokens and $0.50 per 1 million output tokens. You pay only for real token usage, and errors are free. This pricing model is transparent and predictable. You can estimate your costs by counting tokens in your prompts and expected responses.

Users also ask about the trial credit. Every new account receives $0.50 of trial credit without needing a card. This allows you to test the API before purchasing credit with crypto. The trial credit lets you verify that your integration works correctly before committing funds.

Top-ups are done via crypto only. You can use USDT on TRC20 or USDC on Base. The minimum top-up is $20 and the maximum is $500. Credit never expires, so you can top up when prices are favorable. This gives you control over your spending and avoids monthly subscription fees.

Frequently asked questions

How do I authenticate with the API?

You authenticate by passing your API key in the Authorization header. You can obtain this key by signing up with Google or email. The key is shown immediately after signup.

What is the model ID to use?

The model ID is uncensored . This specifies the uncensored open-weight text model. You must use this ID in your requests to get the correct model behavior.

Does the API support streaming?

Yes, the API supports streaming over Server-Sent Events (SSE). You can enable this by setting the stream parameter to true in your request. Usage data is included in the last chunk.

How do I pay for the API?

Payments are accepted via crypto only, specifically USDT on TRC20 or USDC on Base. You can top up any whole amount from $20 to $500. Credit never expires.