# PixelSnapAI API Dart Example
This documentation provides an example code snippet in Dart for using the PixelSnapAI API to upload and analyze an image. Follow the steps below to run the code successfully.
## Prerequisites
Before running the code, ensure that you have the following:
- Dart SDK installed on your system.
## Code Example
```dart
import 'dart:io';
import 'package:http/http.dart' as http;
void main() async {
final url = Uri.parse('URL_END_POINT');
final headers = {
'Authorization': 'Token [SECRET_KEY]',
};
try {
var request = http.MultipartRequest('POST', url)
..headers.addAll(headers)
..files.add(await http.MultipartFile.fromPath(
'picture', './product_img.jpeg',
contentType: MediaType('image', 'jpeg')));
var response = await request.send();
if (response.statusCode == 200) {
var responseData = await response.stream.bytesToString();
// Parse the response JSON
var jsonResponse = json.decode(responseData);
// Extract the relevant information
var message = jsonResponse['message'];
var productNames = jsonResponse['product_name'];
// Print the information
print('Message: $message');
print('Product Names: $productNames');
} else {
print('Error: ${response.reasonPhrase}');
}
} catch (error) {
print('Error: $error');
}
}
Running the Code
Follow these steps to run the provided code:
- Open a text editor or an integrated development environment (IDE) of your choice.
- Copy and paste the code into a new Dart file.
- Make sure you have the image file
product_img.jpeg
available in the same directory as your Dart file. - Replace the placeholder value
'Token [SECRET_KEY]'
in theAuthorization
header with your actual PixelSnapAI API token. This token can be obtained from your PixelSnapAI account after subscribing to a plan. - Save the file with a
.dart
extension, for example,pixelsnapai_example.dart
. - Open a terminal or command prompt and navigate to the directory where the Dart file is saved.
- Run the Dart file by executing the command:
dart pixelsnapai_example.dart
. - The code will send a POST request to the PixelSnapAI API, uploading the image and returning the response.
- The response will be printed to the console, displaying the analysis results or any error messages received from the API.
Return Example
When the API request is successful, the code will receive a JSON response similar to the following:
jsonCopy code{
"message": "Picture uploaded successfully.",
"product_name": [
"Dualshock 4",
"PS4 DualShock 4",
"Pro Wireless GamePad",
"Nuc Controller Plus"
]
}
The returned JSON includes a "message"
field indicating the success status and a "product_name"
field containing a list of product names extracted from the uploaded picture.
Make sure to adjust the code as needed to handle the response according to your application’s requirements.
Conclusion
You have successfully run the Dart example code for utilizing the PixelSnapAI API to upload and analyze an image. This code demonstrates the process of making a POST request to the API endpoint and handling the response.
For further information on the available API endpoints, request parameters, and response formats, please refer to the official PixelSnapAI API documentation on the PixelSnapAI website.
If you encounter any issues or have additional questions, please contact our support team at info@pixelsnapai.com. We are here to assist you!