Code Examples
Ready-to-use code examples for integrating the Legisletter API in JavaScript, Python, and other languages.
Copy-paste examples to get started quickly. No SDK required - just standard HTTP requests.
JavaScript / Node.js
Using fetch (Browser & Node 18+)
const API_KEY = 'll-sk-your-api-key'
async function lookupLegislators(address) {
const response = await fetch(
`https://api.legisletter.org/v1/lookup?address=${encodeURIComponent(address)}`,
{
headers: {
'Authorization': `Bearer ${API_KEY}`
}
}
)
if (!response.ok) {
throw new Error(`API error: ${response.status}`)
}
return response.json()
}
// Usage
const result = await lookupLegislators('350 5th Ave, New York, NY 10118')
console.log(result.representatives.senators)
console.log(result.representatives.representative)Using axios
import axios from 'axios'
const api = axios.create({
baseURL: 'https://api.legisletter.org/v1',
headers: {
'Authorization': 'Bearer ll-sk-your-api-key'
}
})
const { data } = await api.get('/lookup', {
params: { address: '350 5th Ave, New York, NY 10118' }
})
console.log(data.representatives)React Example
import { useState } from 'react'
function FindMyReps() {
const [address, setAddress] = useState('')
const [reps, setReps] = useState(null)
const [loading, setLoading] = useState(false)
async function handleSubmit(e) {
e.preventDefault()
setLoading(true)
const res = await fetch(
`https://api.legisletter.org/v1/lookup?address=${encodeURIComponent(address)}`,
{ headers: { 'Authorization': 'Bearer ll-sk-your-api-key' } }
)
const data = await res.json()
setReps(data.representatives)
setLoading(false)
}
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={address}
onChange={(e) => setAddress(e.target.value)}
placeholder="Enter your address"
/>
<button type="submit" disabled={loading}>
{loading ? 'Loading...' : 'Find My Reps'}
</button>
{reps && (
<div>
<h3>Your Senators</h3>
{reps.senators.map(s => (
<div key={s.bioguideId}>
{s.name} ({s.party}) - {s.phone}
</div>
))}
<h3>Your Representative</h3>
<div>
{reps.representative.name} ({reps.representative.party})
</div>
</div>
)}
</form>
)
}Python
Using requests
import requests
API_KEY = 'll-sk-your-api-key'
BASE_URL = 'https://api.legisletter.org/v1'
def lookup_legislators(address):
response = requests.get(
f'{BASE_URL}/lookup',
params={'address': address},
headers={'Authorization': f'Bearer {API_KEY}'}
)
response.raise_for_status()
return response.json()
# Usage
result = lookup_legislators('350 5th Ave, New York, NY 10118')
for senator in result['representatives']['senators']:
print(f"{senator['name']} ({senator['party']}) - {senator['phone']}")
rep = result['representatives']['representative']
print(f"{rep['name']} ({rep['party']}) - District {rep['district']}")Async with httpx
import httpx
async def lookup_legislators(address):
async with httpx.AsyncClient() as client:
response = await client.get(
'https://api.legisletter.org/v1/lookup',
params={'address': address},
headers={'Authorization': 'Bearer ll-sk-your-api-key'}
)
return response.json()cURL
Basic Request
curl "https://api.legisletter.org/v1/lookup?address=350+5th+Ave,+New+York,+NY+10118" \
-H "Authorization: Bearer ll-sk-your-api-key"POST Request with JSON Body
curl -X POST "https://api.legisletter.org/v1/lookup" \
-H "Authorization: Bearer ll-sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{"address": "350 5th Ave, New York, NY 10118"}'Pretty Print Response
curl "https://api.legisletter.org/v1/lookup?address=350+5th+Ave,+New+York,+NY+10118" \
-H "Authorization: Bearer ll-sk-your-api-key" | jqRuby
require 'net/http'
require 'json'
require 'uri'
API_KEY = 'll-sk-your-api-key'
def lookup_legislators(address)
uri = URI("https://api.legisletter.org/v1/lookup")
uri.query = URI.encode_www_form(address: address)
request = Net::HTTP::Get.new(uri)
request['Authorization'] = "Bearer #{API_KEY}"
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
JSON.parse(response.body)
end
result = lookup_legislators('350 5th Ave, New York, NY 10118')
puts result['representatives']['senators']PHP
<?php
$apiKey = 'll-sk-your-api-key';
$address = urlencode('350 5th Ave, New York, NY 10118');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.legisletter.org/v1/lookup?address={$address}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer {$apiKey}"
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
foreach ($data['representatives']['senators'] as $senator) {
echo "{$senator['name']} ({$senator['party']})\n";
}Go
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const apiKey = "ll-sk-your-api-key"
func lookupLegislators(address string) (map[string]interface{}, error) {
baseURL := "https://api.legisletter.org/v1/lookup"
params := url.Values{}
params.Add("address", address)
req, _ := http.NewRequest("GET", baseURL+"?"+params.Encode(), nil)
req.Header.Add("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
return result, nil
}
func main() {
result, _ := lookupLegislators("350 5th Ave, New York, NY 10118")
fmt.Println(result)
}Need help integrating?
Our team can help you get set up. Book a free integration call.
Book Integration Call