Skip to main content

Creating a request

Use GraphRequest to build a request with a graph path and an optional parameters dictionary. The fields parameter controls which fields the API returns.
import FacebookCore

let request = GraphRequest(
    graphPath: "me",
    parameters: ["fields": "id,name,email"]
)
Starting with Graph API v2.4, GET requests must include an explicit fields parameter. The SDK logs a developer warning if you omit it.

Starting the request

Call start(_:) to fire the request. The completion closure receives a connection, a result, and an error.
request.start { connection, result, error in
    if let error = error {
        print("Request failed:", error.localizedDescription)
        return
    }

    guard let result = result as? [String: Any] else { return }
    let name = result["name"] as? String ?? ""
    print("Hello, \(name)")
}
start(_:) returns an object conforming to GraphRequestConnecting that you can use to cancel the request before it completes.

HTTP methods

The SDK defines three HTTP methods:
ConstantValue
FBSDKHTTPMethodGET"GET"
FBSDKHTTPMethodPOST"POST"
FBSDKHTTPMethodDELETE"DELETE"
GET is the default. To use a different method, pass it during initialisation:
// POST request — e.g. publish a comment
let post = GraphRequest(
    graphPath: "me/feed",
    parameters: ["message": "Hello from my app!"],
    httpMethod: .post
)

post.start { _, result, error in
    // handle result
}
// DELETE request — e.g. remove a post
let delete = GraphRequest(
    graphPath: "<post_id>",
    parameters: [:],
    httpMethod: .delete
)

delete.start { _, result, error in
    // handle result
}

Using a specific access token

By default, GraphRequest reads the token string from AccessToken.current. To supply a different token, use the full initialiser:
let request = GraphRequest(
    graphPath: "me",
    parameters: ["fields": "id,name"],
    tokenString: customTokenString,
    version: nil,
    httpMethod: .get
)
Passing nil for tokenString causes the SDK to fall back to AccessToken.current.

Error handling

The error parameter in the completion handler is an NSError. Check error.userInfo for Graph API-specific details:
request.start { _, result, error in
    if let error = error as NSError? {
        let graphCode = error.userInfo[FBSDKGraphRequestErrorGraphErrorCodeKey]
        let message = error.localizedDescription
        print("Graph error \(graphCode ?? "unknown"): \(message)")
        return
    }
    // process result
}
Key user-info keys:
KeyDescription
FBSDKGraphRequestErrorKeyError category (transient, recoverable, non-recoverable)
FBSDKGraphRequestErrorGraphErrorCodeKeyGraph API error code
FBSDKGraphRequestErrorGraphErrorSubcodeKeyGraph API error subcode
FBSDKGraphRequestErrorHTTPStatusCodeKeyHTTP status code
The SDK automatically attempts error recovery for transient errors when Settings.shared.isGraphErrorRecoveryEnabled is true (the default). You can disable recovery on a per-request basis by setting request.isGraphErrorRecoveryDisabled = true.

Build docs developers (and LLMs) love