Ejemplos Prácticos

Aquí encontrarás ejemplos de código listos para usar para interactuar con la API de flu.lu en diferentes lenguajes.

Ejemplo con cURL

curl -X POST https://api.flu.lu/api/v1/shorten \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'

Ejemplo JavaScript (Fetch)

fetch('https://api.flu.lu/api/v1/shorten', {
     method: 'POST',
     headers: {
       'Content-Type': 'application/json',
       'Authorization': 'Bearer YOUR_API_KEY'
     },
     body: JSON.stringify({url: "https://example.com"})
 })
 .then(response => response.json())
 .then(data => console.log(data));

Ejemplo Python (Requests)

import requests
 headers = {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
 }
 data = {'url': 'https://example.com'}
 response = requests.post('https://api.flu.lu/api/v1/shorten', json=data, headers=headers)
 print(response.json())

Ejemplo PHP (cURL)

<?php
      $url = 'https://api.flu.lu/api/v1/shorten';
      $data = ['url' => 'https://example.com'];

      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
          'Authorization: Bearer YOUR_API_KEY',
          'Content-Type: application/json'
      ]);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

      $response = curl_exec($ch);
      curl_close($ch);

      print_r(json_decode($response));
 ?>

Ejemplo C# (.NET HttpClient)

using System;
 using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;

 class Program {
    static async Task Main(string[] args) {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_API_KEY");

        var content = new StringContent("{\"url\":\"https://example.com\"}", Encoding.UTF8, "application/json");
        var response = await client.PostAsync("https://api.flu.lu/api/v1/shorten", content);

        string result = await response.Content.ReadAsStringAsync();
        Console.WriteLine(result);
    }
 }